gpt4 book ai didi

c++ - 在 MongoDB C++ 中过滤重复项

转载 作者:太空宇宙 更新时间:2023-11-04 14:09:40 24 4
gpt4 key购买 nike

我希望通过根据日期标记重复项来查找我的集合中的所有重复项。以下是我的尝试,但我不确定如何在更新中使用 cmdResult。有什么线索吗?

//filter duplicates
bson::bo cmdResult;
bool ok = c.runCommand(dbcol, BSON("distinct" << "date"), cmdResult);
c.update(dbcol,Query("date"<<cmdResult<<NOT<<"_id"), BSON("$set"<<BSON("noise"<<"true")), false, true);

最佳答案

“distinct”命令将返回集合中所有唯一“日期”值的列表。但您需要的是多次出现的“日期”值列表。

您可以使用聚合命令获取此列表,方法是按“日期”分组并计算条目数,然后匹配计数 > 1:

aggregate([
{ $group: { "_id": "$name", count: {$sum:1} } },
{ $match: { $gt: [ count, 1 ] } }
])

然后您将通过在该列表中查询“日期”并设置“噪声”字段来更新您的集合 (multi:true):

update( {"name": {$in: [<list>]} },{$set: {"noise": true} }, true, false )

有关聚合的帮助,请参阅 http://docs.mongodb.org/manual/reference/aggregation/

关于c++ - 在 MongoDB C++ 中过滤重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15236734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com