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"
}
]
}热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志