2017-03-29 2 views
-2

Wie zu implementieren diese aggregat abfrage in java ich habe keine ahnung, was zu tun ist?mongodb aggregat funktion implementierung in java treiber

db.History.aggregate([ 
    { "$match" : {"thId":"001"}}, 
    { "$group" : { 
       "_id" : {"Id":"$Id","controller" : "$controller","mod" : "$mod","variable" : "$variable"} 
       ,"variable" : {"$first": "$$ROOT"} 
       ,"data" : {"$push":{"value":"$value","updatedDateTime":"$updatedTime"}} 
      } 
    } 
    ]) 
+0

können Sie zeigen, was Sie versucht haben, – radhakrishnan

Antwort

0

Siehe Mongo Java Treiber Documentation,

 MongoClient mongoClient = new MongoClient("localhost",27017); 
     MongoDatabase database = mongoClient.getDatabase("Test"); 
     MongoCollection<Document> collection = database.getCollection("History"); 
     Document doc = new Document(); 
     doc.append("$match",new Document("thId","001")); 
     Document group = new Document(); 

     group.append("$group", new Document("_id",new Document().append("Id", "$Id").append("controller", "$controller").append("mod" , "$mod").append("variable" , "$variable")) 
     .append("variable" , new Document("$first", "$$ROOT")) 
     .append("data",new Document().append("$push", new Document().append("value", "$value").append("updatedDateTime","$updatedTime")))); 
     ArrayList<Document> docList = new ArrayList<Document>(); 
     docList.add(doc); 
     docList.add(group); 

     List<Document> results =collection.aggregate(docList).into(new ArrayList<Document>()); 
     for(Document res: results){ 
      System.out.println(res.toJson()); 
     } 
+0

Sie so viel danken .... – venkat