MongoDB查询中如何更新嵌套文档
> db.demo607.insertOne( ... { ... id:1, ... "Info1" : { ... "Name" : "Chris", ... "Age" : 21, ... ... "Info2" : { ... "SubjectName" : "MongoDB", ... "Marks" : 89 ... } ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e9742a5f57d0dc0b182d62a") }
在find()方法的帮助下显示集合中的所有文档-
> db.demo607.find();
这将产生以下输出-
{ "_id" : ObjectId("5e9742a5f57d0dc0b182d62a"), "id" : 1, "Info1" : { "Name" : "Chris", "Age" : 21, "Info2" : { "SubjectName" : "MongoDB", "Marks" : 89 } } }
以下是更新嵌套文档的查询-
> db.demo607.update({id:1},{$set:{"Info1.Info2.Marks":90}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
在find()方法的帮助下显示集合中的所有文档-
> db.demo607.find().pretty();
这将产生以下输出-
{ "_id" : ObjectId("5e9742a5f57d0dc0b182d62a"), "id" : 1, "Info1" : { "Name" : "Chris", "Age" : 21, "Info2" : { "SubjectName" : "MongoDB", "Marks" : 90 } } }