用于更新嵌套文档的 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"
}
]
}
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP