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 :
db.getCollection('collectionName').updateMany(
{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) {
bulkArr.push({
updateOne: {
filter: { ...{title} },
update: { $inc: { counter: 1 } },
upsert : true
},
});
}
db.collection.bulkWrite(bulkArr);
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 :
You are using
upsert:true
. It will add new documents to collection if your filter query doesn't find any matching documents. SO settingupsert:false
will solve the problem of new documents getting added to collection.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.
EDIT :-
Can you try by removing $set
becoz what I see in latest mongoose docs, $set
is not needed anymore.
This is from mongoose docs.
https://mongoosejs.com/docs/api.html#model_Model.bulkWrite
{
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(
newkeys.map( 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 : "xyz@gmail.com" }, { phone : 9876054321 });
let result = await Model.find({Email : "xyz@gmail.com"}).lean();
console.log(result)
Related Topics
Post Redirect Get Pattern in Rails
How to Use Before :All with Capybara
Accessing a Has_One Associations' Attributes
Ruby Error Reading in Certificate File with Openssl
What's Wrong with the Square and Rectangle Inheritance
Ruby How to Merge Two CSV Files with Slightly Different Headers
How to Include Unit Tests in a Ruby Module
Rails 3 Caching: Expire Action for Named Route
How to Set Up the Recipient Id in Public Activity
Running Selenium::Webdriver::Firefox Inside Xvfb from Ruby as Non-Root User
Overriding Model in Gem, Adding Callback and Methods
Rails Routing - Custom Routes for Resources
Setting a Global Within a Proc
How to Build Ruby 2.1.3 on Osx 10.10 Gm 3.0 with Rbenv