Mongo Shell update operation
-
Exercise data:
db.inventory.insertMany( [ { item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" }, { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" }, { item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" }, { item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" }, { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" }, { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" }, { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" }, { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }, { item: "sketchbook", qty: 80, size: { h: 14, w: 21, uom: "cm" }, status: "A" }, { item: "sketch pad", qty: 95, size: { h: 22.85, w: 30.5, uom: "cm" }, status: "A" } ] );
Update collection document
-
MongoDB provides an update operator to achieve the update effect: Update Operators — MongoDB Manual
-
The update operation has multiple methods, which can update a single document or multiple documents. The parameter structure of the method is similar:
db.coll.function(query_filter, { <update operator>: { <field1>: <value1>, ... }, <update operator>: { <field2>: <value2>, ... }, ... }, )
Behavior of update operation
- All MongoDB writes are atomic operations at the single document level.
- _ id Field, which will not be replaced by the update operation
- Field order:
- _ The id field must be the first field in the document
- The update operator $rename will update the name of the field. Affected by this, the field order of the document may change after updating
- In addition to the above two cases, MongoDB will keep the order of document fields after writing
- Supplementary options: updateOne() updateMany() replaceOne() method has other options besides filtering conditions and updating contents. One of them is $upsert. When $upsert=true and there is no document matching the filtering conditions in the collection, the update operation will create a new document and insert it; If there are matching documents, these methods modify or replace one or more documents.
- Write confirmation: you can specify the confirmation level of the write operation requested by MongoDB. For details, please refer to the write attention under the folder Write Concern - mongodb CN manual (mongoing. Com)
Update method
-
db.collection.updateOne() : update at most one document that matches the filter criteria.
db.collection.updateOne(<filter>, <update>, <project>)
Filter is the filter condition, and update can be the update information containing the update operator or the aggregation pipeline.
-
db.collection.updateMany() : updates all documents that match the filter criteria.
db.collection.function_name(<filter>, <update>, <project>)
The filter criteria and update content are set in the
-
db.collection.replaceOne() : at most one document matching the query criteria can be replaced.
db.collection.function_name(<filter>, <replacement>, <project>)
Is a filter condition, is a replacement content, and cannot contain an update operator; Settings such as upsert are set in
-
db.collection.update() : update or replace a document matching the query criteria by default. You can also update or replace all documents matching the query criteria by setting multi=true.
Other update methods
-
db.collection.findOneAndReplace() : at most one qualified document can be replaced. If upsert is enabled, a document will be inserted when there is no matching document
db.collection.function_name(<filter>, <replacement>, <project>)
Is a filter condition, is a replacement content, and cannot contain an update operator; Settings such as upsert are set in
-
db.collection.findOneAndUpdate() : update a document according to and sort; If there is no document that meets the requirements and upsert is turned on, a new document is created.
db.collection.function_name(<filter>, <update>, <project>)
There is a sort parameter in, which is used to sort the query results of the filter.
db.grades.findOneAndUpdate( { "name" : "A. MacDyver" }, { $inc : { "points" : 5 } }, { sort : { "points" : 1 } } )
-
db.collection.findAndModify({ query: <document>, sort: <document>, remove: <boolean>, update: <document or aggregation pipeline>, // Changed in MongoDB 4.2 new: <boolean>, fields: <document>, upsert: <boolean>, bypassDocumentValidation: <boolean>, writeConcern: <document>, collation: <document>, arrayFilters: [ <filterdocument1>, ... ], let: <document> // Added in MongoDB 5.0 });
This method can only modify one document, even if there are multiple documents that conform to query; If new=false / / the default is false, then this method returns the modified original document. If new=true, then it returns the modified document.
-
db.collection.save() : this method still exists in version 4.2, and other versions have been removed. Update or insert a document based on the set.
db.collection.save( <document>, { writeConcern: <document> } )
If there's nothing in it_ In the ID field, save() actually calls the insert() method. In this process, the mongo shell will create an ObjectId and assign it to_ id;
If there is_ In the id field, save() is equivalent to calling update() and setting upsert=true_ The value of the id field is used as the filter criteria.
-
db.collection.bulkWrite() : perform multiple mongodb writes in sequence
db.collection.bulkWrite( [ <operation 1>, <operation 2>, ... ], { writeConcern : <document>, ordered : <boolean> } )
The first parameter is a list containing multiple write operations, for example:
db.characters.bulkWrite([ { insertOne: { "document": { "_id": 4, "char": "Dithras", "class": "barbarian", "lvl": 4 } } }, { insertOne: { "document": { "_id": 5, "char": "Taeln", "class": "fighter", "lvl": 3 } } }, { updateOne : { "filter" : { "char" : "Eldon" }, "update" : { $set : { "status" : "Critical Injury" } } } }, { deleteOne : { "filter" : { "char" : "Brisbane"} } }, { replaceOne : { "filter" : { "char" : "Meldane" }, "replacement" : { "char" : "Tanys", "class" : "oracle", "lvl": 4 } } } ])
Aggregate pipeline update
- Aggregate pipeline update
- From v4 2 start the update operation using the aggregation pipeline.
- That is, it represents an aggregation pipeline and can also realize the update operation of documents.