How to access insertedIds array of BulkWriteResult in insertMany API of mongoose?











up vote
0
down vote

favorite












insertMany API in mongoose returns a Promise object but i am using the callback version of it.



let options = {
"ordered": true
};

MySchema.insertMany(documents, options, function (error, docs) {
if(error) {
// i need insertedIds array here
} else {
// do someting
}
});


when i stringify the error object, I can see the JSON as shown below:



{
"ok":1,
"writeErrors":[
"code":11000,
"index":1,
// there will be some more failure details here
],
"writeConcernErrors":,
"insertedIds":[
{
"index":0,
"_id":"5be183914d0c761eeadda8c4"
},
{
"index":1,
"_id":"5be183914d0c761eeadda8c5"
},
{
"index":2,
"_id":"5be183914d0c761eeadda8c6"
}
],
"nInserted":1,
"nUpserted":0,
"nMatched":0,
"nModified":0,
"nRemoved":0,
"upserted":
}


but when i try to get the insertedIds array it returns undefined. I did bit of a research and found that the error object returned is actually not a JSON object but something related to BulkWriteResult or BulkWriteError.




So now how can i get the values in insertedIds array? Please help.




P.S.: using mongoose version : ^5.2.17










share|improve this question






















  • { ordered: true } will reject the entire batch of operations on any failure. So nothing gets "inserted" and the key will actually be missing from the returned object. That's by design. See Ordered and Unordered Bulk Operations in the documentation.
    – Neil Lunn
    Nov 7 at 9:23












  • yes i know how ordered works, but setting it to false also does not work
    – Pure'ajax
    Nov 7 at 10:09






  • 1




    Please read How to create a Minimal, Complete, and Verifiable example. You need to produce a "reproducible" example of the problem. So a short listing that inserts some data and errors part way though. Give us a listing so we can all reproduce it ourselves.
    – Neil Lunn
    Nov 7 at 10:15















up vote
0
down vote

favorite












insertMany API in mongoose returns a Promise object but i am using the callback version of it.



let options = {
"ordered": true
};

MySchema.insertMany(documents, options, function (error, docs) {
if(error) {
// i need insertedIds array here
} else {
// do someting
}
});


when i stringify the error object, I can see the JSON as shown below:



{
"ok":1,
"writeErrors":[
"code":11000,
"index":1,
// there will be some more failure details here
],
"writeConcernErrors":,
"insertedIds":[
{
"index":0,
"_id":"5be183914d0c761eeadda8c4"
},
{
"index":1,
"_id":"5be183914d0c761eeadda8c5"
},
{
"index":2,
"_id":"5be183914d0c761eeadda8c6"
}
],
"nInserted":1,
"nUpserted":0,
"nMatched":0,
"nModified":0,
"nRemoved":0,
"upserted":
}


but when i try to get the insertedIds array it returns undefined. I did bit of a research and found that the error object returned is actually not a JSON object but something related to BulkWriteResult or BulkWriteError.




So now how can i get the values in insertedIds array? Please help.




P.S.: using mongoose version : ^5.2.17










share|improve this question






















  • { ordered: true } will reject the entire batch of operations on any failure. So nothing gets "inserted" and the key will actually be missing from the returned object. That's by design. See Ordered and Unordered Bulk Operations in the documentation.
    – Neil Lunn
    Nov 7 at 9:23












  • yes i know how ordered works, but setting it to false also does not work
    – Pure'ajax
    Nov 7 at 10:09






  • 1




    Please read How to create a Minimal, Complete, and Verifiable example. You need to produce a "reproducible" example of the problem. So a short listing that inserts some data and errors part way though. Give us a listing so we can all reproduce it ourselves.
    – Neil Lunn
    Nov 7 at 10:15













up vote
0
down vote

favorite









up vote
0
down vote

favorite











insertMany API in mongoose returns a Promise object but i am using the callback version of it.



let options = {
"ordered": true
};

MySchema.insertMany(documents, options, function (error, docs) {
if(error) {
// i need insertedIds array here
} else {
// do someting
}
});


when i stringify the error object, I can see the JSON as shown below:



{
"ok":1,
"writeErrors":[
"code":11000,
"index":1,
// there will be some more failure details here
],
"writeConcernErrors":,
"insertedIds":[
{
"index":0,
"_id":"5be183914d0c761eeadda8c4"
},
{
"index":1,
"_id":"5be183914d0c761eeadda8c5"
},
{
"index":2,
"_id":"5be183914d0c761eeadda8c6"
}
],
"nInserted":1,
"nUpserted":0,
"nMatched":0,
"nModified":0,
"nRemoved":0,
"upserted":
}


but when i try to get the insertedIds array it returns undefined. I did bit of a research and found that the error object returned is actually not a JSON object but something related to BulkWriteResult or BulkWriteError.




So now how can i get the values in insertedIds array? Please help.




P.S.: using mongoose version : ^5.2.17










share|improve this question













insertMany API in mongoose returns a Promise object but i am using the callback version of it.



let options = {
"ordered": true
};

MySchema.insertMany(documents, options, function (error, docs) {
if(error) {
// i need insertedIds array here
} else {
// do someting
}
});


when i stringify the error object, I can see the JSON as shown below:



{
"ok":1,
"writeErrors":[
"code":11000,
"index":1,
// there will be some more failure details here
],
"writeConcernErrors":,
"insertedIds":[
{
"index":0,
"_id":"5be183914d0c761eeadda8c4"
},
{
"index":1,
"_id":"5be183914d0c761eeadda8c5"
},
{
"index":2,
"_id":"5be183914d0c761eeadda8c6"
}
],
"nInserted":1,
"nUpserted":0,
"nMatched":0,
"nModified":0,
"nRemoved":0,
"upserted":
}


but when i try to get the insertedIds array it returns undefined. I did bit of a research and found that the error object returned is actually not a JSON object but something related to BulkWriteResult or BulkWriteError.




So now how can i get the values in insertedIds array? Please help.




P.S.: using mongoose version : ^5.2.17







node.js mongodb mongoose bulkinsert






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 7:56









Pure'ajax

988




988












  • { ordered: true } will reject the entire batch of operations on any failure. So nothing gets "inserted" and the key will actually be missing from the returned object. That's by design. See Ordered and Unordered Bulk Operations in the documentation.
    – Neil Lunn
    Nov 7 at 9:23












  • yes i know how ordered works, but setting it to false also does not work
    – Pure'ajax
    Nov 7 at 10:09






  • 1




    Please read How to create a Minimal, Complete, and Verifiable example. You need to produce a "reproducible" example of the problem. So a short listing that inserts some data and errors part way though. Give us a listing so we can all reproduce it ourselves.
    – Neil Lunn
    Nov 7 at 10:15


















  • { ordered: true } will reject the entire batch of operations on any failure. So nothing gets "inserted" and the key will actually be missing from the returned object. That's by design. See Ordered and Unordered Bulk Operations in the documentation.
    – Neil Lunn
    Nov 7 at 9:23












  • yes i know how ordered works, but setting it to false also does not work
    – Pure'ajax
    Nov 7 at 10:09






  • 1




    Please read How to create a Minimal, Complete, and Verifiable example. You need to produce a "reproducible" example of the problem. So a short listing that inserts some data and errors part way though. Give us a listing so we can all reproduce it ourselves.
    – Neil Lunn
    Nov 7 at 10:15
















{ ordered: true } will reject the entire batch of operations on any failure. So nothing gets "inserted" and the key will actually be missing from the returned object. That's by design. See Ordered and Unordered Bulk Operations in the documentation.
– Neil Lunn
Nov 7 at 9:23






{ ordered: true } will reject the entire batch of operations on any failure. So nothing gets "inserted" and the key will actually be missing from the returned object. That's by design. See Ordered and Unordered Bulk Operations in the documentation.
– Neil Lunn
Nov 7 at 9:23














yes i know how ordered works, but setting it to false also does not work
– Pure'ajax
Nov 7 at 10:09




yes i know how ordered works, but setting it to false also does not work
– Pure'ajax
Nov 7 at 10:09




1




1




Please read How to create a Minimal, Complete, and Verifiable example. You need to produce a "reproducible" example of the problem. So a short listing that inserts some data and errors part way though. Give us a listing so we can all reproduce it ourselves.
– Neil Lunn
Nov 7 at 10:15




Please read How to create a Minimal, Complete, and Verifiable example. You need to produce a "reproducible" example of the problem. So a short listing that inserts some data and errors part way though. Give us a listing so we can all reproduce it ourselves.
– Neil Lunn
Nov 7 at 10:15

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185423%2fhow-to-access-insertedids-array-of-bulkwriteresult-in-insertmany-api-of-mongoose%23new-answer', 'question_page');
}
);

Post as a guest





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185423%2fhow-to-access-insertedids-array-of-bulkwriteresult-in-insertmany-api-of-mongoose%23new-answer', 'question_page');
}
);

Post as a guest




















































































這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings