在MongoDB中将字段添加到数组中的嵌入式文档中?
您可以update()与$运算符一起使用。首先让我们创建一个包含文档的集合-
> db.addAFieldDemo.insertOne(
... {
...
... "ClientName" : "Larry",
... "ClientCountryName" : "US",
... "ClientOtherDetails" : [
... {
... "ClientProjectName":"Online Banking System"
... }
... ]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd44bdc2cba06f46efe9ee8")
}以下是在find()方法的帮助下显示集合中所有文档的查询-
> db.addAFieldDemo.find().pretty();
这将产生以下输出-
{
"_id" : ObjectId("5cd44bdc2cba06f46efe9ee8"),
"ClientName" : "Larry",
"ClientCountryName" : "US",
"ClientOtherDetails" : [
{
"ClientProjectName" : "Online Banking System"
}
]
}以下是将字段添加到数组中的嵌入式文档的查询-
> db.addAFieldDemo.update({ClientOtherDetails:{$elemMatch:{"ClientProjectName" : "Online Banking System"}}},
... {$set :{'ClientOtherDetails.$.isMarried':true}},true);
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })让我们显示上述集合中的所有文档-
> db.addAFieldDemo.find().pretty();
这将产生以下输出-
{
"_id" : ObjectId("5cd44bdc2cba06f46efe9ee8"),
"ClientName" : "Larry",
"ClientCountryName" : "US",
"ClientOtherDetails" : [
{
"ClientProjectName" : "Online Banking System",
"isMarried" : true
}
]
}热门推荐
1 训练狗狗搜寻
2 如何训练狗狗装死
3 狗狗训练书
6 怎么训练狗狗拜年
8 狗狗训练游戏
10 狗狗嗅觉训练
11 怎么训练狗狗在笼子里不叫
12 怎么训练狗狗站立走路
13 怎么训练狗狗在马桶上
14 狗狗尿尿怎么训练
15 训练狗狗用品
16 泰迪狗狗的训练方法大小便
17 训练狗狗可以打吗
18 王曼丽的狗狗训练