MongoDB uses Update to update records

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…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索