如何在MongoDB中更新并在数组中插入新元素

时间:2020-09-13 09:51:41

标签: mongodb

我是MongoDB的新手。我想将用户数据存储在数组中。数据是单个唯一的文档。可以这样说。

User { id: number, name: string }
{
  _id : 001,
  room: User[],
}

我想做这件事...

  • 如果没有room[],则推送元素(用户)。
  • 更新用户数据。

就像更新操作中有upsert选项一样。

  DB.Collection.updateOne(
      { _id: "001" },
      { $set: { name: "Bar" } },
      { upsert: true }
    );

如何在文档数组中执行类似的操作?

2 个答案:

答案 0 :(得分:2)

您可以使用$set操作用$(update)$[$[<identifier>]更新嵌入式文档。但是,upsert的效果不好,因此要插入新文档,请使用{{1 }}。请执行一些操作以分别更新数据或追加数据。

$push

答案 1 :(得分:0)

//you can use update with $set or $push based on whether you want to update one element in array with override/update or push another element.
> db.rooms.find();
{ "_id" : 1, "room" : [ "raj" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2, "room" : [ "raj" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3, "room" : [ "raj" ] }
> db.rooms.updateMany(   {},   {$set:{"room":["john"]}} );
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.rooms.find();
{ "_id" : 1, "room" : [ "john" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2, "room" : [ "john" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3, "room" : [ "john" ] }
> db.rooms.updateMany(   {},   {$push:{"room":["mike"]}} );
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.rooms.find();
{ "_id" : 1, "room" : [ "john", [ "mike" ] ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2, "room" : [ "john", [ "mike" ] ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3, "room" : [ "john", [ "mike" ] ] }
> db.rooms.updateMany(   {},   {$push:{"room":"mike"}} );
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.rooms.find();
{ "_id" : 1, "room" : [ "john", [ "mike" ], "mike" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2, "room" : [ "john", [ "mike" ], "mike" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3, "room" : [ "john", [ "mike" ], "mike" ] }
>

推送将在文档中添加一个字段(如果该字段不存在),因此不需要专门检查字段空间。

> db.rooms.find();
{ "_id" : 1, "room" : [ "john", [ "mike" ], "mike" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2, "room" : [ "john", [ "mike" ], "mike" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3, "room" : [ "john", [ "mike" ], "mike" ] }
> db.rooms.updateMany(
...   {},
...   {$unset:{"user":""}}
... );
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 0 }
> db.rooms.updateMany(   {},   {$unset:{"room":""}} );
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.rooms.find();
{ "_id" : 1 }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2 }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3 }
> db.rooms.updateMany(   {},   {$push:{"room":"mike"}} );
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.rooms.find();
{ "_id" : 1, "room" : [ "mike" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2, "room" : [ "mike" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3, "room" : [ "mike" ] }
>