mongTemplate聚合匹配并投影一个没有id的字段

问题描述 投票:0回答:1

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

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

在Java中

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

List<String> fields= mongoTemplate.aggregate(aggregation, "Collection", BasicDBObject.class)
        .getMappedResults();
java mongodb spring-data-mongodb
1个回答
0
投票

感谢@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();
© www.soinside.com 2019 - 2024. All rights reserved.