Upsert Multiple Records with Mongodb

How to update and upsert multiple documents in MongoDB using C# Drivers

After Mongo 2.6 you can do Bulk Updates/Upserts. Example below does bulk update using c# driver.

MongoCollection<foo> collection = database.GetCollection<foo>(collectionName);
var bulk = collection.InitializeUnorderedBulkOperation();
foreach (FooDoc fooDoc in fooDocsList)
var update = new UpdateDocument { {fooDoc.ToBsonDocument() } };
bulk.Find(Query.EQ("_id", fooDoc.Id)).Upsert().UpdateOne(update);
BulkWriteResult bwr = bulk.Execute();

MongoDB: How to update multiple documents with a single command?

Multi update was added recently, so is only available in the development releases (1.1.3). From the shell you do a multi update by passing true as the fourth argument to update(), where the the third argument is the upsert argument:

db.test.update({foo: "bar"}, {$set: {test: "success!"}}, false, true);

For versions of mongodb 2.2+ you need to set option multi true to update multiple documents at once.

db.test.update({foo: "bar"}, {$set: {test: "success!"}}, {multi: true})

For versions of mongodb 3.2+ you can also use new method updateMany() to update multiple documents at once, without the need of separate multi option.

db.test.updateMany({foo: "bar"}, {$set: {test: "success!"}})

MongoDB - update multiple documents solution - update document counter or insert if doesn't exist

So if you've multiple filters then you would normally use $in operator, So if you do like :

{title: {$in : ["some title", "some title 3"]}},
{$inc : {counter: 1}}, {upsert : true})

Above, query would only update matching documents but doesn't do upsert. Since you've multiple filters then you can take advantage of .bulkwrite() which can have a group of operations up to 100k.

let bulkArr = [];

let titles = ["some title", "some title3"];

for (const title of titles) {
updateOne: {
filter: { ...{title} },
update: { $inc: { counter: 1 } },
upsert : true


This .bulkWrite() would also perform multiple update operations on database but by using this you can avoid doing multiple DB calls.

How to update multiple records with multiple values in mongo db and node js?

Some of the things I notice here are :

  1. You are using upsert:true. It will add new documents to collection if your filter query doesn't find any matching documents. SO setting upsert:false will solve the problem of new documents getting added to collection.

  2. Another problem I suspect is in following line of code.

filter: { ID: parsedRes[i].id },

I think it should be id instead of ID (I am assuming it is not a Javascript constant).
If you have const ID = "id" somewhere in your code then ignore this point.


Can you try by removing $set becoz what I see in latest mongoose docs, $set is not needed anymore.

This is from mongoose docs.

updateOne: {
filter: { name: 'Eddard Stark' },
// If you were using the MongoDB driver directly, you'd need to do
// `update: { $set: { title: ... } }` but mongoose adds $set for
// you.
update: { title: 'Hand of the King' }

update multi records and insert if not present

Your thinking on this is the wrong way around. Instead you take the "array" and turn that into operations. Instead of sending multiple "requests" you send multiple "operations" in "one request". With Bulk Operations using .bulkWrite()

var newKeys = ["fruit123", "fruit1234" ];

db.multiArr.bulkWrite( key => {
return {
"updateOne": {
"filter": { "ID": key },
"update": { "$set": { "Keys": "tomato" } },
"upsert": true

Here using a regular JavaScript .map() to transform the array content of newKeys into an array of "bulk write operations" statements to send to the server. In other languages the basic concept remains the same.

Over the wire, the send and response with the server is done in "one request", whilst the "package" sent for execution contains multiple actions.

If needed the return of this method in all API's is a BulkWriteResult which contains details of matched documents, updates and upserts as appropriate.

update multiple records in mongodb using mongoose

if the Model created correctly, just try

await Model.updateMany({ Email : "" }, { phone : 9876054321 });
let result = await Model.find({Email : ""}).lean();

Related Topics

Leave a reply