【问题标题】:Filter duplicates in MongoDB C++在 MongoDB C++ 中过滤重复项
【发布时间】:2013-02-20 14:49:11
【问题描述】:

我希望通过根据日期标记重复项来查找我的收藏中的所有重复项。以下是我的尝试,但我不确定如何在更新中使用 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);

【问题讨论】:

    标签: c++ mongodb duplicates


    【解决方案1】:

    “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/

    【讨论】:

      猜你喜欢
      • 2020-12-03
      • 2021-09-27
      • 2021-09-11
      • 1970-01-01
      • 2020-08-12
      • 2014-03-12
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      相关资源
      最近更新 更多