3

我正在尝试使用 mongo c 驱动程序来更新单个文档中的嵌套数组。这是一个示例测试文档:

{ 
  "_id" : ObjectId("562416504bacd3940b8b2d5c"), 
  "folder1" : [
    {
      "folder_id" : "5624200d4bacd3940b8b2d62", 
      "some" : "data"
    }, 
    {
      "folder_id" : "562940084bacf60575d3b17e", 
      "some" : "data"
    }
  ], 
  "folder2" : [
    {
      "folder_id" : "5627e20d4bacefccf4864e4e", 
      "some" : "data"
    }
  ]
}

我想遍历两者"folder1""folder2"寻找任何"folder_id"与特定字符串(EX:)匹配的内容5624200d4bacd3940b8b2d62。然后我希望从数组中删除整个元素。因此,删除后我的文档应如下所示:

{ 
  "_id" : ObjectId("562416504bacd3940b8b2d5c"), 
  "folder1" : [
    {
      "folder_id" : "562940084bacf60575d3b17e", 
      "some" : "data"
    }
  ], 
  "folder2" : [
    {
      "folder_id" : "5627e20d4bacefccf4864e4e", 
      "some" : "data"
    }
  ]
}

我知道如何修改文档中的单个元素,示例如下。但是我还没有找到任何迭代嵌套数组元素的好例子。

update = BCON_NEW ("$set", "{","some_cool_key", BCON_UTF8 ("some cool data for key"),"}");
mongoc_collection_update (collection, MONGOC_UPDATE_NONE, query, update, NULL, &error);
4

1 回答 1

1

我想感谢 John Moore @ ObjectRocket 在这方面帮助我。他提供了一个简单的单行 BCON,它完全符合我的需要:

update = BCON_NEW ("$pull", "{", "folder1", "{", "folder_id", BCON_UTF8 ("5624200d4bacd3940b8b2d62"), "}", "}");
于 2015-11-08T01:08:25.777 回答