有没有一种简单的方法来"$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: "$.*"},
}}
https://stackoverflow.com/questions/22150205
复制相似问题