Mongo分组查询转换为java
我应该如何在java中转换下面的mongo查询,我使用了mongo java驱动程序Mongo分组查询转换为java,java,mongodb,Java,Mongodb,我应该如何在java中转换下面的mongo查询,我使用了mongo java驱动程序 db.demo.aggregate([ // Unwind the array { "$unwind": "$iInfo" }, // Sort the array elements within documents { "$sort": { "_id": -1, "iInfo.ifout": -1 } }, // Take only the "first" array element per docume
db.demo.aggregate([
// Unwind the array
{ "$unwind": "$iInfo" },
// Sort the array elements within documents
{ "$sort": { "_id": -1, "iInfo.ifout": -1 } },
// Take only the "first" array element per document
{ "$group": {
"_id": "$_id",
"Iifout": { "$first": "$iInfo.ifout" },
"Iiferror": { "$first": "$iInfo.iferror" },
"Iifdes": { "$first": "$iInfo.ifdes" },
"Iifin": { "$first": "$iInfo.ifin" }
}},
// Group to push those results as an array
{ "$group": {
"_id": "$_id",
"iInfo": {
"$push": {
"ifout": "$Iifout",
"iferror": "$Iiferror",
"Iifdes": "$Iifdes",
"Iifin": "$Iifin"
}
}
}}
])
我按如下方式编写java代码,但它不能正常工作
BasicDBObject cmdBody = new BasicDBObject("aggregate",
collectionRealtime.toString());
pipeline.add(new BasicDBObject("$limit", 10));
pipeline.add(new BasicDBObject("$unwind", "$iInfo"));
pipeline.add(new BasicDBObject("$sort", new BasicDBObject(
"iInfo.ifout", -1)));
cmdBody.put("pipeline", pipeline);
当我运行上述java代码时,它会显示我所有的嵌套输出,带有排序,但不在组中显示,限制不工作。它会显示所有文档。这真的不难。它只是构造基本的CDBObObject项并将它们添加到列表表单中,或者作为单独的参数。你在这里的尝试并不是真正的尝试。您可以随时使用System.out.println或其他更好的日志来查看您构建的内容。真的,在要求别人为您编写复制/粘贴代码之前,请试一试。这里是另一个关于良好实践的提示。不要用从你尚未接受的答案中收集的代码作为发布另一个问题的手段。您引用的答案实际上解决了您的问题,即使您仍在编写Java代码。您可以随时询问发布特定Java示例答案的人。哦,等等,那是我。