如何使用匹配和投影进行聚合。投影包含一个字段,但不包含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();
答案 0 :(得分: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();