Same as above, then learn MongoDB’s data update syntax.
1. Update a record
–1.1 Query documents with id=1
> db.test_2.find({id:1}); { “_id” : ObjectId(“50a0d46ceb825d827b0c3f9b”), “id” : 1, “name” : “francs” } { “_id” : ObjectId(“50a12e34f73f2e4aa1ff71bc”), “id” : 1, “name” : “francs” } |
–1.2 Update the name field of documents with id=1 as
‘name_2’
> db.test_2.update({id:1},{$set:{name:’name_2′}}); |
–1.3 Query documents with id=1 again
> db.test_2.find({id:1}); { “_id” : ObjectId(“50a0d46ceb825d827b0c3f9b”), “id” : 1, “name” : “name_2” } { “_id” : ObjectId(“50a12e34f73f2e4aa1ff71bc”), “id” : 1, “name” : “francs” } |
Remarks: Only one matching record was found to be updated.
2. Update multiple records
–2.1 Query documents with id=2
> db.test_2.find({id:2}); { “_id” : ObjectId(“50a0d46ceb825d827b0c3f9c”), “id” : 2, “name” : “fpZhou” } { “_id” : ObjectId(“50a12e7cf73f2e4aa1ff71bd”), “id” : 2, “name” : “fpZhou” } { “_id” : ObjectId(“50a12f47f73f2e4aa1ff71be”), “id” : 2, “name” : “fpZhou” } |
–2.2 Update the name field of documents with id=2 to ‘name_3’
> db.test_2.update({id:2},{$set:{name:’name_3′}},{multi:true}); |
–2.3 Query documents with id=2 again
> ;db.test_2.find({id:2}); { “_id” : ObjectId(“50a0d46ceb825d827b0c3f9c”), “id” : 2, “name” : “name_3” } { “_id” : ObjectId(“50a12e7cf73f2e4aa1ff71bd”), “id” : 2, “name” : “name_3” } { “_id” : ObjectId(“50a12f47f73f2e4aa1ff71be”), “id” : 2, “name” : “name_3”} |
Note: By default, update() only updates the first matching record. If you want to update multiple records, you need to set
The multi parameter is
true.
3. The case where the updated record does not exist
–3.1 Query documents with id=6
> db.test_2.find({id:6}); |
Remarks: The record with id=6 does not exist.
–3.2 Update without upsert parameters
> db.test_2.update({id:6},{$set:{name:’name_6′}}); > db.test_2.find({id:6}); > |
Remarks: After the update at this time, there is no new record with id=6.
–3.3 Updating with upsert parameters
> db.test_2.update({id:6},{$set:{name:’name_6′}},{upsert:true}); > db.test_2.find({id:6}); { “_id” : ObjectId(“50a1328f7543857379c2bb38”), “id” : 6, “name” : “name_6” } > |
Remarks: If the updated document does not exist, you can determine whether to insert it by specifying the upsert parameter
A new record is inserted if it is true, otherwise, it is not inserted.
Four. Attachment
Append one .update() syntax
db.collection.update( criteria, objNew, upsert, multi
)
criteria – query which selects the record to
update;
objNew – updated object or $ operators
(e.g., $inc) which manipulate the
object
upsert – if this should be an “upsert”
operation; that is, if the record(s) do
Not
exist, insert one. Upsert only inserts a single document.
multi – indicates if all
documents matching criteria should be updated rather
than
just one. Can be useful with the $ operators below.
If you are coming from SQL, be aware
that by default, update() only modifies
The first matched object. If you want to modify
all matched objects, you need
to use the multi flag.
Appendix 2. $set Syntax
Use the $set operator to set a particular value.
The $set operator requires the
following syntax:
db. collection. update( { field: value1 }, { $set: {
field1: value2 } } );
This statement updates in the document in collection
where field matches value1 by
replacing the value of the field field1 with value2.
will add the speci
filed field or fields if they do not exist in this document or
replace the existing
value of the specified field(s) if they already exist.