【问题标题】:MongoDB deleting aggregated cellsMongoDB删除聚合单元格
【发布时间】:2016-10-23 19:17:55
【问题描述】:

我有问题。

我收集了这样的文件:

{
  id (not _id),
  type,
  number
}

我想要做的是聚合具有特定类型的单元格和每个 id 的该类型的最小数量,并从该集合中删除它们。基本上,单个 id 对于特定类型可以有几个不同的数字,我想删除具有最低值的 Document。

我尝试使用 Java Driver 3 和 mongoshell 来聚合它,但我坚持构建它。

【问题讨论】:

    标签: mongodb aggregation-framework mongodb-java mongo-java-driver


    【解决方案1】:

    你可以参考这样的东西

    List<DBObject> pipeline=new ArrayList<DBObject>();
    DBObject match = new BasicDBObject("$match", new BasicDBObject("date", sdf.format(new Date())).append("country", country).append("operator", operator).append("server_ip", server_ip));
    DBObject unwind = new BasicDBObject("$unwind", "$details");
    DBObject match2 = new BasicDBObject("$match", new BasicDBObject("details.type", "application_health"));
    DBObject sort = new BasicDBObject("$sort", new BasicDBObject("details.datetime", -1));
    DBObject limit = new BasicDBObject("$limit", 1);
    pipeline.add(match);
    pipeline.add(unwind);
    pipeline.add(match2);
    pipeline.add(sort);
    pipeline.add(limit);
    AggregationOutput outputoutput = collection.aggregate(pipeline);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 2019-09-21
      • 1970-01-01
      • 2022-08-13
      • 1970-01-01
      相关资源
      最近更新 更多