2017-02-20 71 views
0

转换shell命令的Java代码,MongoDB的聚集命令java代码

喜试图做的是什么IAM,集团回收的 “sourceSystemName”,并得到LOGID,类型,_id,sourceSystemName的值,logTime Description该条为MAX“ logTime Description该条”

收集样本数据:(含100万数据)

{ "logID" : "1487408645950", "logTime" : ISODate("2017-02-6T06:47:59Z"), "type" : "SYSTEM_MONITOR", "sourceSystemId" :"192.168.1.226", "sourceSystemName" : "LOADER.LOG" } 

{ "logID" : "1488226732268", "logTime" : ISODate("2017-02-16T06:48:00Z"),"type" : "SYSTEM_MONITOR", "sourceSystemId" :"192.168.1.226", "sourceSystemName" : "PLATFORM.LOG" } 

{ "logID" : "1488293048361", "logTime" : ISODate("2017-02-16T06:48:01Z"),"type" : "SYSTEM_MONITOR", "sourceSystemId" :"192.168.1.226", "sourceSystemName" : "PLATFORM.LOG" } 

{ "logID" : "1487496165381", "logTime" : ISODate("2017-02-16T06:48:03Z"),"type" : "SYSTEM_MONITOR", "sourceSystemId" :"192.168.1.226", "sourceSystemName" : "LOADER.LOG" } 

任务:

GROUP by "sourceSystemName" 

get values of logID,type,_id,sourceSystemName,logTime for MAX "logTime" 

ExpectedOutput:在MongoShell

{ "_id" : "LOADER.LOG", "logTime" : ISODate("2017-02-16T20:44:06Z"), "result" : [ { "sourceSystemName" : "LOADER.LOG", "_id" : ObjectId("58a686bb1a20043138d47ecb"), "logID" : "1488673221443", "type" : "SYSTEM_MONITOR", "logTime" : ISODate("2017-02-16T20:44:06Z") } ] } 

{ "_id" : "PLATFORM.LOG", "logTime" : ISODate("2017-02-16T08:42:25Z"), "result" : [ { "sourceSystemName" : "PLATFORM.LOG", "_id" : ObjectId("58a565f61a20041b81aa4017"), "logID" : "1487834117661", "type" : "SYSTEM_MONITOR", "logTime" : ISODate("2017-02-16T08:42:25Z") } ] } 

使用的命令:

db.log_system_monitoring.aggregate([{ 
    "$group": { 
     "_id": "$sourceSystemName", 
     "logTime": { 
      "$max": "$logTime" 
     }, 
     "result": { 
      "$push": { 
       "_id": "$_id", 
       "logID": "$logID", 
       "type": "$type", 
       "logTime": "$logTime" 
      } 
     } 
    } 
}, { 
    "$project": { 
     "logTime": 1, 
     "result": { 
      "$setDifference": [{ 
        "$map": { 
         "input": "$result", 
         "as": "result", 
         "in": { 
          "$cond": [{ 
            "$eq": ["$logTime", "$$result.logTime"] 
           }, 
           "$$result", 
           false 
          ] 
         } 
        } 
       }, 
       [false] 
      ] 
     } 
    } 
}]) 

现在我需要这个命令转换成Java代码,问题是,我不知道如何$ setDifference对象追加到文档( )对象mongodb java驱动程序。任何人都可以帮助,

如果有任何其他更好的解决方案,这个输出建议我。

+0

请告诉我们你有什么到目前为止已经试过让我们帮助你。 – Veeram

+0

嗨@Veeram我改进了我的问题,为此提出了一些解决方案, – radhakrishnan

+0

你的mongo服务器版本是什么? – Veeram

回答

1

你可以尝试下面的东西。

List<Document> results = 
      collection.aggregate(
     Arrays.asList(
       Aggregates.group(
         "$sourceSystemName", 
         max("logTime", "$logTime"), 
         push("result", 
           new Document("_id", "$id"). 
             append("logID", "$logID"). 
             append("type", "$type"). 
             append("logTime", "$logTime")) 
       ), 
       Aggregates.project(
         fields(include("logTime"), 
           new Document("result", 
             new Document("$setDifference", 
               Arrays.asList(
                 new Document("$map", 
                   new Document("input", "$result"). 
                     append("as", "result"). 
                     append("in", 
                       new Document("$cond", 
                         Arrays.asList(new Document("$eq", Arrays.asList("$logTime", "$$result.logTime")), 
                           "$$result", 
                           false) 
                       ) 
                     ) 
                 ), 
                 Arrays.asList(false) 
               ) 
             ) 
           ) 
         ) 
       ) 
     ) 
).into(new ArrayList<>()); 

OR

String maxResult = "{\n" + 
    "\t\"$setDifference\": [{\n" + 
    "\t\t\t\"$map\": {\n" + 
    "\t\t\t\t\"input\": \"$result\",\n" + 
    "\t\t\t\t\"as\": \"result\",\n" + 
    "\t\t\t\t\"in\": {\n" + 
    "\t\t\t\t\t\"$cond\": [{\n" + 
    "\t\t\t\t\t\t\t\"$eq\": [\"$logTime\", \"$$result.logTime\"]\n" + 
    "\t\t\t\t\t\t},\n" + 
    "\t\t\t\t\t\t\"$$result\",\n" + 
    "\t\t\t\t\t\tfalse\n" + 
    "\t\t\t\t\t]\n" + 
    "\t\t\t\t}\n" + 
    "\t\t\t}\n" + 
    "\t\t},\n" + 
    "\t\t[false]\n" + 
    "\t]\n" + 
    "}"; 

List<Document> results = collection.aggregate(Arrays.asList(Aggregates.group("$sourceSystemName", max("logTime", "$logTime"), push("result", new Document("_id", "$id"). 
       append("logID", "$logID"). 
       append("type", "$type"). 
       append("logTime", "$logTime"))), Aggregates.project(fields(include("logTime"), new Document("result", Document.parse(maxResult))) 
))).into(new ArrayList<>()); 

或更好的办法是使用$filter

String letFilter = "{\n" + 
    "\t$let: {\n" + 
    "\t\tvars: {\n" + 
    "\t\t\tlogTimeMax: {\n" + 
    "\t\t\t\t$max: \"$result.logTime\"\n" + 
    "\t\t\t}\n" + 
    "\t\t},\n" + 
    "\t\tin: {\n" + 
    "\t\t\t$filter: {\n" + 
    "\t\t\t\tinput: \"$result\",\n" + 
    "\t\t\t\tas: \"result\",\n" + 
    "\t\t\t\tcond: {\n" + 
    "\t\t\t\t\t$eq: [\"$$result.logTime\", '$$logTimeMax']\n" + 
    "\t\t\t\t}\n" + 
    "\t\t\t}\n" + 
    "\t\t}\n" + 
    "\t}\n" + 
    "}"; 

List<Document> results = collection.aggregate(Arrays.asList(Aggregates.group("$sourceSystemName", push("result", new Document("_id", "$id"). 
         append("logID", "$logID"). 
         append("type", "$type"). 
         append("logTime", "$logTime"))), Aggregates.project(new Document("result", Document.parse(letFilter))) 
     )).into(new ArrayList<>()); 
+0

嗨@Veeram你能告诉我哪个版本的mongodb java驱动程序用于上述回答, – radhakrishnan

+0

我已经使用3.4.1。你遇到问题了吗? – Veeram

+0

是的,我有一些问题推和最大显示未定义的错误。 – radhakrishnan