Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

heterogeneous bulk update in mongodb

Tags:

mongodb

I know that we can bulk update documents in mongodb with

db.collection.update( criteria, objNew, upsert, multi )

in one db call, but it's homogeneous, i.e. all those documents impacted are following one kind of criteria. But what I'd like to do is something like

db.collection.update([{criteria1, objNew1}, {criteria2, objNew2}, ...]

, to send multiple update request which would update maybe absolutely different documents or class of documents in single db call.

What I want to do in my app is to insert/update a bunch of objects with compound primary key, if the key is already existing, update it; insert it otherwise.

Can I do all these in one combine in mongodb?

like image 286
Jason Yang Avatar asked Oct 19 '11 10:10

Jason Yang


People also ask

How to Bulk update records in MongoDB?

We can use either initializeOrderedBulkOp () or initializeUnorderedBulkOp () method to perform bulk operations in MongoDB. We need to use bulk. execute () method to run all the operations which was built by bulk methods. From MongoDB version, 4.2 bulk update methods will accept the pipeline aggregation.

How do I update multiple fields in MongoDB?

Update Multiple Fields of a Single Document. We can use $set and $inc operators to update any field in MongoDB. The $set operator will set the newly specified value while the $inc operator will increase the value by a specified value.

What is bulk operation in MongoDB?

MongoDB provides clients the ability to perform write operations in bulk. Bulk write operations affect a single collection. MongoDB allows applications to determine the acceptable level of acknowledgement required for bulk write operations.

How to find and update in MongoDB?

MongoDB – db. collection. findOneAndUpdate() Method. The findOneAndUpdate() method updates the first matched document in the collection that matches the selection criteria.


2 Answers

That's two seperate questions. To the first one; there is no MongoDB native mechanism to bulk send criteria/update pairs although technically doing that in a loop yourself is bound to be about as efficient as any native bulk support.

Checking for the existence of a document based on an embedded document (what you refer to as compound key, but in the interest of correct terminology to avoid confusion it's better to use the mongo name in this case) and insert/update depending on that existence check can be done with upsert :

document A :

{
    _id: ObjectId(...),
    key: {
        name: "Will",
        age: 20
    }
}

db.users.update({name:"Will", age:20}, {$set:{age: 21}}), true, false)

This upsert (update with insert if no document matches the criteria) will do one of two things depending on the existence of document A :

  • Exists : Performs update "$set:{age:21}" on the existing document
  • Doesn't exist : Create a new document with fields "name" and field "age" with values "Will" and "20" respectively (basically the criteria are copied into the new doc) and then the update is applied ($set:{age:21}). End result is a document with "name"="Will" and "age"=21.

Hope that helps

like image 149
Remon van Vliet Avatar answered Nov 02 '22 23:11

Remon van Vliet


we are seeing some benefits of $in clause. our use case was to update the 'status' in a document for a large number number records. In our first cut, we were doing a for loop and doing updates one by 1. But then we switched to using $in clause and that made a huge improvement.

like image 30
verma Avatar answered Nov 03 '22 00:11

verma