蒙戈文件:
{
"_id" : "1",
"array" : [
{
"item" : "item"
},
{
"item" : "item"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我的mongo shell query看起来像这样:
db.getCollection('collectionName').aggregate(
{$match: { _id: "1"}},
{$project: { count: { $size:"$array" }}}
)
Run Code Online (Sandbox Code Playgroud)
无论如何要使用Mongo Template from Spring?
到目前为止,我有这个:
MatchOperation match = new MatchOperation(Criteria.where("_id").is("1"));
ProjectionOperation project = new ProjectionOperation();
Aggregation aggregate = Aggregation.newAggregation(match, project);
mongoTemplate.aggregate(aggregate, collectionName, Integer.class);
Run Code Online (Sandbox Code Playgroud)
我想我只是缺少project逻辑,但我不确定是否可以在$size or equivalent这里做。
java mongodb spring-data mongodb-query aggregation-framework