如何替换MongoDB中的整个子文档数组?

时间:2014-03-06 02:44:52

标签: mongodb mongodb-query mongodb-.net-driver

以下是我的收藏中的示例文档:

Books
[
   id: 1,
   links:
   [
     {text: "ABC", "url": "www.abc.com"},
     {text: "XYZ", "url": "www.xyz.com"}
   ]
]

我想在一次更新操作中替换links数组。以下是如何修改上述文档的示例:

Books
[
   id: 1,
   links:
   [
     {text: "XYZ", "url": "www.xyz.com"},
     {text: "efg", "url": "www.efg.com"},   <== NEW COPY OF THE ARRAY
     {text: "ijk", "url": "www.ijk.com"}
   ]
]

正如您所看到的,链接数组已被替换(删除了旧数据,并添加了新数据)。

我很难使用Update.Set(),因为它说MyLinks<>无法映射到BsonValue

我尝试了许多不同的方法来实现这一点,并且所有方法都失败了,包括.PushAllWrapped<WebLinkRoot>("links", myDoc.WebLinks)。 我尝试过的所有内容都会导致新值附加到数组中,而不是被替换的数组。

因为看起来MongoDB没有提供一个简单的方法来替换子文档数组或像.ClearArray()这样的方法,在单个文件中添加新元素之前,确保清除数组的最佳方法是什么查询?

2 个答案:

答案 0 :(得分:7)

我在这里是因为我在此帖子上看到了5k的浏览量,我添加的内容可能对其他寻求以上答案的人有所帮助

 db.collectionName.insertOne({
      'links': [
                 {
                     "text" : "XYZ",
                      "url" : "www.xyz.com"
                  }
        ]
  });

现在运行此查询,以帮助替换较早的数据

db.collectionName.update(
      {
        _id: ObjectId("your object Id")
      }, 
      {
        $set: 
         {
          'links':[ {
                     "text" : "XYZ1",
                     "url" : "www.xyz.com1"
                   } ]
         }
      });

答案 1 :(得分:1)

我认为你必须做这样的事情:

var newArray = new BSONArray {
    new BSONDocument { { "text", "XYZ" }, { "url", "www.xyz.com" } },
    new BSONDocument { { "text", "efg" }, { "url", "www.efg.com" } },
    new BSONDocument { { "text", "ijk" }, { "url", "www.ijk.com" } }
};

var update = Update.Set( "links", newArray );

collection.Update( query, update );

或者您可以将任何方法强制转换为有效的BSONValue。

等同于shell:

{ "links" : [ { "text" : "abc" } ] }

> db.collection.update(
      {},
      { $set: 
          { links: [ 
              { text: "xyz", url: "something" },
              { text: "zzz", url: "else" }
          ]}
      })

>db.collection.find({},{ _id: 0, links:1 }).pretty()

{ "links" : [
            {
                    "text" : "xyz",
                    "url" : "something"
            },
            {
                    "text" : "zzz",
                    "url" : "else"
            }
    ]
}

这样有效。

除了嵌入式代码,您显然还需要其他东西。但希望这会让你走上正轨。