我完全感到困惑(并且非常沮丧(。如何使用 mongoc 库创建此调用?
我在集合中有以下文档结构
{_id: myOID,
subsriptions: {
newProducts: true,
newBlogPosts: true,
pressReleases: true,
}
}
我想删除其中一个订阅,例如,用户不再希望收到我的新闻稿。
这在 mongo shell 中有效。现在我需要用 C 代码来做
updateOne({_id: myOID}, [{'$unset': 'subscriptions.pressReleases'}], {})
请注意 Mongo shell 中的 update 参数是一个匿名数组。我需要为在 mongoc_collection_update_one(( API 调用中作为更新参数传入的 bson 执行此操作。
更新的 C 代码一是
mongo_status = mongoc_collection_update_one (mongo_collection,
mongo_query,
mongo_update,
NULL, /* No Opts to pass in */
NULL, /* no reply wanted */
&mongo_error);
另请注意,在 aggregate(( API 中,这是使用
{"pipeline" : [{'$unset': 'elists.lunch' }] }
updateOne(( shell 函数和 mongoc_collection_update_one(( API 调用都不接受这一点,它们只想要数组。
如何创建 bson 以用作 mongoc_collection_update_one(( API 调用的第二个参数?
乔的答案有效,我能够完成我需要做的事情。
$unset更新运算符获取对象,就像$set一样。
updateOne({_id: myOID},{'$unset':{'subscriptions.pressReleases': true}})
或者甚至更好
updateOne({_id: myOID},{'$unset':{'subscriptions.pressReleases': {'$exists': true}}})
这将删除订阅标志,无论该字段的值是什么。
这样做不需要匿名数组(我仍然不知道如何创建(。