我正在尝试获取按标签数量排序的出版物列表。我已经完成了一些工作,但是$unwind
操作员使带有零标签的出版物消失了。我试图添加一个占位符来绕过它但没有成功:
Publication.collection.aggregate(
{ "$project" => { tags: { "$push" => "holder" } } },
{ "$unwind" => '$tags' },
{ "$group" => { _id: '$_id', count: { "$sum" => 1 } } },
{ "$sort" => { count: 1 } }
)
我有:
failed with error 15999: "exception: invalid operator '$push'"
文档示例:
{ _id: '1', tags: ['b','c'] }
{ _id: '2', tags: ['a'] }
{ _id: '3' }
有任何想法吗?