MongoDB查询更新嵌套文档
让我们创建一个包含文档的集合-
> db.demo595.insertOne( { "Information": [   { "_id": new ObjectId(), Name:"Chris" },
   { _id:new ObjectId(), Name:"Robert" }
] } );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e93369cfd2d90c177b5bce4")
}
在find()方法的帮助下显示集合中的所有文档-
> db.demo595.find().pretty();
这将产生以下输出-
{   "_id" : ObjectId("5e93369cfd2d90c177b5bce4"),
   "Information" : [
      {
         "_id" : ObjectId("5e93369cfd2d90c177b5bce2"),
         "Name" : "Chris"
      },
      {
         "_id" : ObjectId("5e93369cfd2d90c177b5bce3"),
         "Name" : "Robert"
      }
   ]
}
以下是更新嵌套文档的查询-
>db.demo595.update({"Information._id":ObjectId("5e93369cfd2d90c177b5bce2")},   {$set:{"Info rmation.$.Name":"David Miller"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
在find()方法的帮助下显示集合中的所有文档-
> db.demo595.find().pretty();
这将产生以下输出-
{   "_id" : ObjectId("5e93369cfd2d90c177b5bce4"),
   "Information" : [
      {
         "_id" : ObjectId("5e93369cfd2d90c177b5bce2"),
         "Name" : "David Miller"
      },
      {
         "_id" : ObjectId("5e93369cfd2d90c177b5bce3"),
         "Name" : "Robert"
      }
   ]
}
以上是 MongoDB查询更新嵌套文档 的全部内容, 来源链接: utcz.com/z/317180.html

