通过查询将Mongo组转换为java

时间:2014-07-08 10:02:54

标签: java mongodb

如何在java中转换下面的mongo查询,我使用了mongo java driver

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代码上面运行时,它会向我展示我的所有嵌套输出,但不是在组中显示,而是限制不显示所有文档。

0 个答案:

没有答案