mongo c 驱动程序更新嵌套数组
mongo c driver update nested array
我正在尝试使用 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"
(例如: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);
我想感谢 John Moore @ObjectRocket 帮助我解决这个问题。他提供了一个简单的单行 BCON,完全符合我的需要:
update = BCON_NEW ("$pull", "{", "folder1", "{", "folder_id", BCON_UTF8 ("5624200d4bacd3940b8b2d62"), "}", "}");
我正在尝试使用 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"
(例如: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);
我想感谢 John Moore @ObjectRocket 帮助我解决这个问题。他提供了一个简单的单行 BCON,完全符合我的需要:
update = BCON_NEW ("$pull", "{", "folder1", "{", "folder_id", BCON_UTF8 ("5624200d4bacd3940b8b2d62"), "}", "}");