仅从猫鼬组/聚集操作中返回原始文档 [英] Return original documents only from mongoose group/aggregation operation
问题描述
我对一堆文件(书)进行了过滤+分组操作.分组将仅返回共享相同book_id
(名称)的书籍的最新版本.下面的代码可以工作,但是由于返回了多余的信息,因此很麻烦:
I have a filter + group operation on a bunch of documents (books). The grouping is to return only latest versions of books that share the same book_id
(name). The below code works, but it's untidy since it returns redundant information:
return Book.aggregate([
{ $match: generateMLabQuery(rawQuery) },
{
$sort: {
"published_date": -1
}
},
{
$group: {
_id: "$book_id",
books: {
$first: "$$ROOT"
}
}
}
])
我最终得到了一个看起来像这样的对象数组:
I end up with an array of objects that looks like this:
[{ _id: "aedrtgt6854earg864", books: { singleBookObject } }, {...}, {...}]
基本上,我只需要singleBookObject
部分,这是原始文档(以及如果仅执行$match
操作所得到的内容).有没有办法消除聚合管道中的冗余_id
和books
部分?
Essentially I only need the singleBookObject
part, which is the original document (and what I'd be getting if I had done only the $match
operation). Is there a way to get rid of the redundant _id
and books
parts within the aggregation pipeline?
推荐答案
您可以使用
这篇关于仅从猫鼬组/聚集操作中返回原始文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!