有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java模板聚合匹配和投影一个没有id的字段

如何使用匹配和投影运行聚合。投影包括一个字段,不包括id

db.collection("Collection").aggregate([
    {
        $match : {
            "someCriteriaFlag" : false
        }
    },
    {
        $project : {
            "field1" : 1,
            "_id" : 0
        }
    }
]);

在爪哇

Aggregation aggregation = Aggregation.newAggregation(
        Aggregation.match(Criteria.where("someCriteriaFlag").is(false)), 
        Aggregation.project("field1"));

List<String> fields= mongoTemplate.aggregate(aggregation, "Collection", BasicDBObject.class)
        .getMappedResults();

共 (1) 个答案

  1. # 1 楼答案

    感谢@NeilLunn

    Aggregation aggregation = Aggregation.newAggregation(
        Aggregation.match(Criteria.where("someCriteriaFlag").is(false)), 
        Aggregation.project("field1").andExclude("_id"));
    
    List<String> fields= mongoTemplate.aggregate(aggregation, "Collection", BasicDBObject.class)
        .getMappedResults();