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
insertedIdsarray? Please help.
P.S.: using mongoose version : ^5.2.17
node.js mongodb mongoose bulkinsert
add a comment |
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
insertedIdsarray? Please help.
P.S.: using mongoose version : ^5.2.17
node.js mongodb mongoose bulkinsert
{ 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
add a comment |
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
insertedIdsarray? Please help.
P.S.: using mongoose version : ^5.2.17
node.js mongodb mongoose bulkinsert
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
insertedIdsarray? Please help.
P.S.: using mongoose version : ^5.2.17
node.js mongodb mongoose bulkinsert
node.js mongodb mongoose bulkinsert
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
add a comment |
{ 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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
{ 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