使MongoDB用字符串替换单个数组值?
> db.demo564.insertOne({"StudentName":["Chris","David","Mike","Sam"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e90880a39cfeaaf0b97b576") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo564.find().pretty();
这将产生以下输出-
{ "_id" : ObjectId("5e90880a39cfeaaf0b97b576"), "StudentName" : [ "Chris", "David", "Mike", "Sam" ] }
以下是用字符串替换单个数组值的查询-
> db.demo564.updateMany( ... { "StudentName": "David" }, ... { "$set": { "StudentName.$": "Carol Taylor" } } ... ) { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 } >
在find()
方法的帮助下显示集合中的所有文档-
> db.demo564.find().pretty();
这将产生以下输出-
{ "_id" : ObjectId("5e90880a39cfeaaf0b97b576"), "StudentName" : [ "Chris", "Carol Taylor", "Mike", "Sam" ] }