如何仅更新MongoDB中的一个属性?
要仅更新一个属性,请在MongoDB中使用$addToSet。让我们创建一个包含文档的集合-
> db.demo336.insertOne({"Name":"Chris","Score":[45,67,78]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e522cb1f8647eb59e562097")
}
> db.demo336.insertOne({"Name":"David","Score":[89,93,47]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e522cb2f8647eb59e562098")
}在find()方法的帮助下显示集合中的所有文档-
> db.demo336.find();
这将产生以下输出-
{ "_id" : ObjectId("5e522cb1f8647eb59e562097"), "Name" : "Chris", "Score" : [ 45, 67, 78 ] }
{ "_id" : ObjectId("5e522cb2f8647eb59e562098"), "Name" : "David", "Score" : [ 89, 93, 47 ] }以下是仅更新MongoDB中的一个属性的查询-
> db.demo336.update({Name:"David"},{ $addToSet: {Score: [56,34,71] }});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })在find()方法的帮助下显示集合中的所有文档-
> db.demo336.find();
这将产生以下输出-
{ "_id" : ObjectId("5e522cb1f8647eb59e562097"), "Name" : "Chris", "Score" : [ 45, 67, 78 ] }
{ "_id" : ObjectId("5e522cb2f8647eb59e562098"), "Name" : "David", "Score" : [ 89, 93, 47, [ 56, 34, 71 ] ] }热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短