使用java mongo驱动程序在java中实现mongodb查询

时间:2015-05-20 11:53:23

标签: java mongodb mongo-java-driver

我使用下面的mongo查询来获取最高温度。任何人都可以使用BasicDBObjectDBObject帮助使用mongo java驱动程序在java中实现吗?

db.EventLog.aggregate(
   [
     {
       $group:
         {
           _id: "$_id",
           maxInnerTemp: { $max: { $concat : [ "0", "$fields.innerTemp"]}}
         }
     }
   ]
)

1 个答案:

答案 0 :(得分:0)

使用mongo java aggregation代码如下(未经测试):

 // $group operation
 BasicDBList concat = new BasicDBList();
 concat.add("0");
 concat.add("$fields.innerTemp");
 DBObject groupFields = new BasicDBObject("_id", "$_id");
 groupFields.put("maxInnerTemp", new BasicDBObject("$max", new BasicDBObject("$concat", concat)); 
 DBObject group = new BasicDBObject("$group", groupFields);
     // run aggregation
     List < DBObject > pipeline = Arrays.asList(group); AggregationOutput output = collectionName.aggregate(pipeline);
     for (DBObject result: output.results()) {
       System.out.println(result);
     }