我有一个实体User
:
{
"_id" : ObjectId("584d863e439e512f4c457eea"),
"_class" : "edu.aabramov.model.User",
"username" : "e",
"todos" : [
{
"_id" : ObjectId("584d8654439e512f4c457eeb"),
"title" : "Officiis id sed aut molestiae id id magni.",
"body" : "Fugit velit rem incidunt. Et ad aliquam inventore voluptatem sed omnis voluptas. Aut sint eum voluptatem.",
"when" : ISODate("1972-09-15T04:24:08.880Z"),
"priority" : "HIGH",
"status" : "DEFAULT",
"tags" : [
"animi",
"veniam",
"exercitationem"
]
},
// ...
]
}
当我尝试使用MongoTemplate进行聚合时:
public List<Todo> getUserTodos(String userId) {
TypedAggregation<User> aggregation = newAggregation(
User.class, Aggregation.match(Criteria.where("_id").is(userId)),
Aggregation.unwind("todos")
);
AggregationResults<Todo> groupResults = mongoTemplate.aggregate(aggregation, User.class, Todo.class);
return groupResults.getMappedResults();
}
查询返回没有字段的文档(Java中的对象):
[
{
"id": "584d56bb439e5158ed5e5716",
"title": null,
"body": null,
"when": null,
"priority": null,
"status": null,
"tags": null
}
// ...
]
在mongo cli中使用相同的查询:
db.users.aggregate([
{ $match: { "_id" : ObjectId("584d56bb439e5158ed5e5716") } },
{ $unwind: "$todos" }
])
所有文件都有字段值。
除了使用Aggregation.project
预测每个字段外,还有其他方法吗?