Insert documents into collection unless they are already inserted?











up vote
3
down vote

favorite












I get a list of objects from an API:



let sold = [
{ objId: 3240747,
soldDate: '2018-09-27',
soldPrice: 4610000,
apartmentNumber: '1202',
soldPriceSource: 'bid',
},
{ objId: 3234263,
soldDate: '2018-09-24',
soldPrice: 2580000,
soldPriceSource: 'bid',
}
...
]


I store these in a collection:



soldCollection.insertMany(sold)


Some of the objects have been retrieved before, and I only want to store the once that are not already in the database.



 dbo.collection("sold").createIndex({ "objId": 1 }, { unique: true })


What would be an efficient way of doing this? Should I ask for each object before storing it or is there method for dealing with this?










share|improve this question






















  • You can just insert into the database since you are adding a unique key. Mongodb will not insert if it already exists. Check for errors if you want to do something extra if it already exists, otherwise just ignore the error.
    – Get Off My Lawn
    Nov 10 at 20:19












  • And if memory serves me correctly there is an option in insertMany on how to handle errors as in stop or ignore
    – charlietfl
    Nov 10 at 20:20

















up vote
3
down vote

favorite












I get a list of objects from an API:



let sold = [
{ objId: 3240747,
soldDate: '2018-09-27',
soldPrice: 4610000,
apartmentNumber: '1202',
soldPriceSource: 'bid',
},
{ objId: 3234263,
soldDate: '2018-09-24',
soldPrice: 2580000,
soldPriceSource: 'bid',
}
...
]


I store these in a collection:



soldCollection.insertMany(sold)


Some of the objects have been retrieved before, and I only want to store the once that are not already in the database.



 dbo.collection("sold").createIndex({ "objId": 1 }, { unique: true })


What would be an efficient way of doing this? Should I ask for each object before storing it or is there method for dealing with this?










share|improve this question






















  • You can just insert into the database since you are adding a unique key. Mongodb will not insert if it already exists. Check for errors if you want to do something extra if it already exists, otherwise just ignore the error.
    – Get Off My Lawn
    Nov 10 at 20:19












  • And if memory serves me correctly there is an option in insertMany on how to handle errors as in stop or ignore
    – charlietfl
    Nov 10 at 20:20















up vote
3
down vote

favorite









up vote
3
down vote

favorite











I get a list of objects from an API:



let sold = [
{ objId: 3240747,
soldDate: '2018-09-27',
soldPrice: 4610000,
apartmentNumber: '1202',
soldPriceSource: 'bid',
},
{ objId: 3234263,
soldDate: '2018-09-24',
soldPrice: 2580000,
soldPriceSource: 'bid',
}
...
]


I store these in a collection:



soldCollection.insertMany(sold)


Some of the objects have been retrieved before, and I only want to store the once that are not already in the database.



 dbo.collection("sold").createIndex({ "objId": 1 }, { unique: true })


What would be an efficient way of doing this? Should I ask for each object before storing it or is there method for dealing with this?










share|improve this question













I get a list of objects from an API:



let sold = [
{ objId: 3240747,
soldDate: '2018-09-27',
soldPrice: 4610000,
apartmentNumber: '1202',
soldPriceSource: 'bid',
},
{ objId: 3234263,
soldDate: '2018-09-24',
soldPrice: 2580000,
soldPriceSource: 'bid',
}
...
]


I store these in a collection:



soldCollection.insertMany(sold)


Some of the objects have been retrieved before, and I only want to store the once that are not already in the database.



 dbo.collection("sold").createIndex({ "objId": 1 }, { unique: true })


What would be an efficient way of doing this? Should I ask for each object before storing it or is there method for dealing with this?







javascript node.js mongodb ecmascript-6






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 20:11









Himmators

5,4812684171




5,4812684171












  • You can just insert into the database since you are adding a unique key. Mongodb will not insert if it already exists. Check for errors if you want to do something extra if it already exists, otherwise just ignore the error.
    – Get Off My Lawn
    Nov 10 at 20:19












  • And if memory serves me correctly there is an option in insertMany on how to handle errors as in stop or ignore
    – charlietfl
    Nov 10 at 20:20




















  • You can just insert into the database since you are adding a unique key. Mongodb will not insert if it already exists. Check for errors if you want to do something extra if it already exists, otherwise just ignore the error.
    – Get Off My Lawn
    Nov 10 at 20:19












  • And if memory serves me correctly there is an option in insertMany on how to handle errors as in stop or ignore
    – charlietfl
    Nov 10 at 20:20


















You can just insert into the database since you are adding a unique key. Mongodb will not insert if it already exists. Check for errors if you want to do something extra if it already exists, otherwise just ignore the error.
– Get Off My Lawn
Nov 10 at 20:19






You can just insert into the database since you are adding a unique key. Mongodb will not insert if it already exists. Check for errors if you want to do something extra if it already exists, otherwise just ignore the error.
– Get Off My Lawn
Nov 10 at 20:19














And if memory serves me correctly there is an option in insertMany on how to handle errors as in stop or ignore
– charlietfl
Nov 10 at 20:20






And if memory serves me correctly there is an option in insertMany on how to handle errors as in stop or ignore
– charlietfl
Nov 10 at 20:20














1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










By default insertMany will stop inserting when first error occurs (E11000 duplicate key error in this case). You can change that behavior by specifying ordered parameter set to false. In that case you'll get a list of errors from failed inserts however all valid documents will be inserted succesfully:



db.sold.insertMany(sold, { ordered: false })


docs example here






share|improve this answer





















  • cool, I hadn't even noticed it worked at all. Is there some way of disabling this specific error? Either for this operation of in general.
    – Himmators
    Nov 10 at 20:34










  • Unfortunately there's no INSERT IF NOT EXISTS in MongoDB however with unordered I think this can be considered more like a warning than error, it only informs you that some inserts failed however entire "batch" is processed succesfully
    – mickl
    Nov 10 at 20:36











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%2f53243000%2finsert-documents-into-collection-unless-they-are-already-inserted%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










By default insertMany will stop inserting when first error occurs (E11000 duplicate key error in this case). You can change that behavior by specifying ordered parameter set to false. In that case you'll get a list of errors from failed inserts however all valid documents will be inserted succesfully:



db.sold.insertMany(sold, { ordered: false })


docs example here






share|improve this answer





















  • cool, I hadn't even noticed it worked at all. Is there some way of disabling this specific error? Either for this operation of in general.
    – Himmators
    Nov 10 at 20:34










  • Unfortunately there's no INSERT IF NOT EXISTS in MongoDB however with unordered I think this can be considered more like a warning than error, it only informs you that some inserts failed however entire "batch" is processed succesfully
    – mickl
    Nov 10 at 20:36















up vote
1
down vote



accepted










By default insertMany will stop inserting when first error occurs (E11000 duplicate key error in this case). You can change that behavior by specifying ordered parameter set to false. In that case you'll get a list of errors from failed inserts however all valid documents will be inserted succesfully:



db.sold.insertMany(sold, { ordered: false })


docs example here






share|improve this answer





















  • cool, I hadn't even noticed it worked at all. Is there some way of disabling this specific error? Either for this operation of in general.
    – Himmators
    Nov 10 at 20:34










  • Unfortunately there's no INSERT IF NOT EXISTS in MongoDB however with unordered I think this can be considered more like a warning than error, it only informs you that some inserts failed however entire "batch" is processed succesfully
    – mickl
    Nov 10 at 20:36













up vote
1
down vote



accepted







up vote
1
down vote



accepted






By default insertMany will stop inserting when first error occurs (E11000 duplicate key error in this case). You can change that behavior by specifying ordered parameter set to false. In that case you'll get a list of errors from failed inserts however all valid documents will be inserted succesfully:



db.sold.insertMany(sold, { ordered: false })


docs example here






share|improve this answer












By default insertMany will stop inserting when first error occurs (E11000 duplicate key error in this case). You can change that behavior by specifying ordered parameter set to false. In that case you'll get a list of errors from failed inserts however all valid documents will be inserted succesfully:



db.sold.insertMany(sold, { ordered: false })


docs example here







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 20:32









mickl

10.1k51435




10.1k51435












  • cool, I hadn't even noticed it worked at all. Is there some way of disabling this specific error? Either for this operation of in general.
    – Himmators
    Nov 10 at 20:34










  • Unfortunately there's no INSERT IF NOT EXISTS in MongoDB however with unordered I think this can be considered more like a warning than error, it only informs you that some inserts failed however entire "batch" is processed succesfully
    – mickl
    Nov 10 at 20:36


















  • cool, I hadn't even noticed it worked at all. Is there some way of disabling this specific error? Either for this operation of in general.
    – Himmators
    Nov 10 at 20:34










  • Unfortunately there's no INSERT IF NOT EXISTS in MongoDB however with unordered I think this can be considered more like a warning than error, it only informs you that some inserts failed however entire "batch" is processed succesfully
    – mickl
    Nov 10 at 20:36
















cool, I hadn't even noticed it worked at all. Is there some way of disabling this specific error? Either for this operation of in general.
– Himmators
Nov 10 at 20:34




cool, I hadn't even noticed it worked at all. Is there some way of disabling this specific error? Either for this operation of in general.
– Himmators
Nov 10 at 20:34












Unfortunately there's no INSERT IF NOT EXISTS in MongoDB however with unordered I think this can be considered more like a warning than error, it only informs you that some inserts failed however entire "batch" is processed succesfully
– mickl
Nov 10 at 20:36




Unfortunately there's no INSERT IF NOT EXISTS in MongoDB however with unordered I think this can be considered more like a warning than error, it only informs you that some inserts failed however entire "batch" is processed succesfully
– mickl
Nov 10 at 20:36


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243000%2finsert-documents-into-collection-unless-they-are-already-inserted%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues