MongoDB $ aggregate $ push Java Spring Data中的多个字段 [英] MongoDB $aggregate $push multiple fields in Java Spring Data
本文介绍了MongoDB $ aggregate $ push Java Spring Data中的多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个mongo聚合组查询:
I have a mongo aggregate group query:
db.wizard.aggregate(
{
$group: {
_id: "$title",
versions: { $push: {version:"$version", author:"$author", dateAdded:"$dateAdded"}}
}
})
我在Java Spring-Data-MongoDB中需要这个查询,我目前的解决方案如下所示:
I need this query in Java Spring-Data-MongoDB, my current solution looks like this:
Aggregation agg = Aggregation.newAggregation(
Aggregation.group("title").
push("version").as("versions")
);
问题是我不知道如何为push方法添加更多字段(版本,作者,添加日期)。
是否可以使用Spring-Data-MongoDB?
Problem is that i don't know how to add more fields to push method (version, author, dateAdded). Is it possible with Spring-Data-MongoDB?
推荐答案
您可以直接将BasicDbObject传递给任何聚合管道阶段。
You can directly pass the BasicDbObject to any of the aggregation pipeline stage.
Aggregation agg = newAggregation(
group("title").
push(new BasicDBObject
("version", "$version").append
("author", "$author").append
("dateAdded", "$dateAdded")).as("versions"));
这篇关于MongoDB $ aggregate $ push Java Spring Data中的多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文