Mongo小组并推:推动所有领域 [英] Mongo group and push: pushing all fields
本文介绍了Mongo小组并推:推动所有领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种简单的方法可以"$ push"文档的所有字段? 例如:
Is there an easy way to "$push" all fields of a document? For example:
说我有一个Mongo藏书:
Say I have a Mongo collection of books:
{author: "tolstoy", title:"war & peace", price:100, pages:800}
{author: "tolstoy", title:"Ivan Ilyich", price:50, pages:100}
我想按作者将它们分组-为每位作者列出他的整个图书对象:
I'd like to group them by author - for each author, list his entire book objects:
{ author: "tolstoy",
books: [
{author: "tolstoy", title:"war & peace", price:100, pages:800}
{author: "tolstoy", title:"Ivan Ilyich", price:50, pages:100}
]
}
我可以通过显式推送所有字段来实现此目的:
I can achieve this by explicitly pushing all fields:
{$group: {
_id: "$author",
books:{$push: {author:"$author", title:"$title", price:"$price", pages:"$pages"}},
}}
但是有什么捷径吗?
// Fictional syntax...
{$group: {
_id: "$author",
books:{$push: "$.*"},
}}
推荐答案
您可以使用$$ ROOT
You can use $$ROOT
{ $group : {
_id : "$author",
books: { $push : "$$ROOT" }
}}
在这里找到:如何使用mongodb聚合并检索全部文档
这篇关于Mongo小组并推:推动所有领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文