有没有一种简单的方法来“ $push”文档的所有字段? 例如:
假设我有一套 Mongo 系列的书:
{author: "tolstoy", title:"war & peace", price:100, pages:800}
{author: "tolstoy", title:"Ivan Ilyich", price:50, pages:100}
我想把它们按作者分组——对于每个作者,列出他的 完整的图书对象:
{ author: "tolstoy",
books: [
{author: "tolstoy", title:"war & peace", price:100, pages:800}
{author: "tolstoy", title:"Ivan Ilyich", price:50, pages:100}
]
}
我可以通过显式地推送所有字段来实现这一点:
{$group: {
_id: "$author",
books:{$push: {author:"$author", title:"$title", price:"$price", pages:"$pages"}},
}}
但是有没有什么捷径,比如:
// Fictional syntax...
{$group: {
_id: "$author",
books:{$push: "$.*"},
}}