目前,我能够基于filePrefix进行分组,并获得具有最大fileId的记录。

在输出中,我能够获取filePrefix和fileId,但无法获取fileName以及filePrefix和fileId。

谁能帮我解决这个问题...

我的文档:

    {
            "_id" : ObjectId("58cbe224238b3953da3bc0bc"),
            "fileName" : "samplefile1_124.txt",
            "filePrefix":"samplefile1",
            "fileId":124
    }
    {
            "_id" : ObjectId("58cbe257238b3953da3bc0bd"),
            "fileName" : "samplefile2_125.txt",
            "filePrefix":"samplefile2",
            "fileId":125

    }
    {
            "_id" : ObjectId("58cf8d13f731b796bc343726"),
            "fileName" : "samplefile3_126.dat",
            "filePrefix":"samplefile3",
            "fileId":126
    }
    {
            "_id" : ObjectId("58cfa525f731b796bc343727"),
            "fileName" : "samplefile1_126.txt",
            "filePrefix":"samplefile1",
            "fileId":126
    }
    {
            "_id" : ObjectId("58cfa525f731b796bc343728"),
            "fileName" : "samplefile2_127.txt",
            "filePrefix":"samplefile2",
            "fileId":127

    }


我的代码:

    MongoClient mongo = new MongoClient("localhost",27017);
    MongoDatabase db = mongo.getDatabase("fileDB");
    MongoCollection<Document> col =  db.getCollection("fileStatus");

    List<String> docsList = new ArrayList<>();
    docsList.add("samplefile1");
    docsList.add("samplefile2");
    docsList.add("samplefile3");

    Set<String> docsSet = new HashSet<>();
    for(String st: docsList){
        docsSet.add(st);
        }

    Document match =  new Document("$match", new Document("filePrefix",new Document("$in",docsSet)));
    Document group =  new Document("$group" , new Document("_id","$filePrefix").append("fId", new Document("$max","$fileId")));
    Document project = new Document("$project",new Document("filePrefix","$_id").append("fileId", "$fId"));

     AggregateIterable<Document> output = col.aggregate(Arrays.asList(match,group,project));

        for (Document dbObject : output)
        {
            System.out.println(dbObject);
        }


我的输出:


  文档{{_id = samplefile1,filePrefix = samplefile1,fileId = 126.0}}
  文档{{_id = samplefile2,filePrefix = samplefile2,fileId = 127.0}}
  文档{{_id = samplefile3,filePrefix = samplefile3,fileId = 126.0}}

最佳答案

您需要运行以下聚合管道,该管道在应用组操作之前首先对文档进行排序,因为如果要返回,则需要使用$first(如果是降序)或$last(如果是升序)其他领域。从本质上讲,这将为您提供max字段以及相应的document字段。

例如,使用样本文档作为输入

db.test.aggregate([
    { "$match": { "filePrefix": { "$in": docsSet }  } },
    { "$sort": { "filePrefix": 1, "fileId": -1 } },
    {
        "$group": {
            "_id": "$filePrefix",
            "fileId": { "$first": "$fileId"},
            "fileName": { "$first": "$fileName"}
        }
    }
])


即翻译为

Document match =  new Document(
    "$match", new Document("filePrefix",
        new Document("$in", docsSet)
    )
);
Document sort =  new Document("$sort",
    new Document("filePrefix", 1).append("fileId", -1)
);
Document group =  new Document("$group" ,
    new Document("_id", "$filePrefix")
        .append("fileId",  new Document("$first", "$fileId")
        .append("fileName",  new Document("$first", "$fileName")
    )
);
Document project = new Document("$project",
    new Document("filePrefix", "$_id")
        .append("fileId", 1)
        .append("fileName", 1)
);

10-05 18:41