java驱动程序中mongodb聚合函数的实现
如何在java中实现这个聚合查询我不知道该怎么做java驱动程序中mongodb聚合函数的实现,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,如何在java中实现这个聚合查询我不知道该怎么做 db.History.aggregate([ { "$match" : {"thId":"001"}}, { "$group" : { "_id" : {"Id":"$Id","controller" : "$controller","mod" : "$mod","variable" : "$variable"} ,"variable" : {"$first": "$
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"}}
}
}
])
参考Mongo Java驱动程序
MongoClient-MongoClient=newmongoclient(“localhost”,27017);
MongoDatabase=mongoClient.getDatabase(“测试”);
MongoCollection collection=database.getCollection(“历史”);
单据单据=新单据();
附加文件($match),新文件(“thId”,“001”);
文档组=新文档();
group.append(“$group”,新文档(“\u id”,new Document()。append(“id”,“$id”)。append(“controller”,“controller”)。append(“mod”,“$mod”)。append(“variable”,“variable”))
.append(“变量”,新文档(“$first”,“$$ROOT”))
.append(“data”,new Document().append($push),new Document().append(“value”,“$value”).append(“updatedDateTime”,“$updatedTime”)));
ArrayList docList=新的ArrayList();
docList.add(doc);
添加(组);
列表结果=collection.aggregate(docList).into(new ArrayList());
用于(文档资源:结果){
System.out.println(res.toJson());
}
您能展示一下您的尝试吗,
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());
}