【问题标题】:MongoDB aggregation code using the Java driver使用 Java 驱动程序的 MongoDB 聚合代码
【发布时间】:2018-02-06 15:35:35
【问题描述】:

我很难将 MongoDB 聚合查询转换为 Java。我能够在 Mongo shell 中得到结果,但在 Java 中却没有……

db.intentAnalysisPojo.aggregate(
    {"$match": { "threadId": "f5683604-9fc8-40dd-9b7b-a1ef8e3bdb02"} },
    { "$group": {
        "_id":"$question", 
        "answer": { "$first": "$answer" },
        "intent": { "$first": "$intent" },
        "wdsAns1":{"$first": "$wdsAns1"},
        "wdsAns2":{"$first": "$wdsAns2"},
        "wdsAns3":{"$first": "$wdsAns3"},
        }},
    { "$sort" : { "intent" : 1}}
);

我尝试了这段 Java 代码,但它不工作......

Document firstGroup = new Document("$group",
    new Document("_id", "$question")
        .append("$first", "$answer")
        .append("$first", "$intent")
        .append("$first", "$wdsAns1")
        .append("$first", "$wdsAns2")
        .append("$first", "$wdsAns3"));

AggregationOperation match = Aggregation.match(Criteria.where("threadId").is(uuid));

AggregationOperation group = Aggregation.group(firstGroup.toJson());

AggregationOperation sort = Aggregation.sort(Sort.Direction.ASC, "intent");

谢谢

【问题讨论】:

    标签: java mongodb mongodb-query aggregation-framework mongodb-java


    【解决方案1】:

    这是您问题中与 MongoDB shell 命令等效的 MongoDB Java 驱动程序:

    MongoClient mongoClient = ...;
    
    MongoCollection<Document> collection = mongoClient.getDatabase("...").getCollection("...");
    
    List<Document> documents = collection.aggregate(Arrays.asList(
            // {"$match": { "threadId": "f5683604-9fc8-40dd-9b7b-a1ef8e3bdb02"} }
            Aggregates.match(new Document("threadId", "f5683604-9fc8-40dd-9b7b-a1ef8e3bdb02")),
    
            // { "$group": { "_id":"$question", "answer": { "$first": "$answer" }, "intent": { "$first": "$intent" }, "wdsAns1":{"$first": "$wdsAns1"}, "wdsAns2":{"$first": "$wdsAns2"}, "wdsAns3":{"$first": "$wdsAns3"} }}
            new Document("$group",
                    new Document("_id", "$question")
                            .append("answer", new Document("$first", "$answer"))
                            .append("intent", new Document("$first", "$intent"))
                            .append("wdsAns1", new Document("$first", "$wdsAns1"))
                            .append("wdsAns2", new Document("$first", "$wdsAns2"))
                            .append("wdsAns3", new Document("$first", "$wdsAns3"))
            ),
    
            // { "$sort" : { "intent" : 1} }
            Aggregates.sort(new Document("$sort", new Document("intent", new BsonInt32(1)))))
    ).into(new ArrayList<>());
    
    for (Document document : documents) {
        logger.info("{}", document.toJson());
    }
    

    注意事项:

    • 这是使用 v3.x 的 Java 驱动程序
    • 此代码故意使用new Document("$group", ...) 习惯用法而不是Aggregates.group() 实用程序来帮助阐明shell 命令和Java 等效命令之间的转换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      相关资源
      最近更新 更多