仅从猫鼬组/聚集操作中返回原始文档 [英] Return original documents only from mongoose group/aggregation operation

查看:63
本文介绍了仅从猫鼬组/聚集操作中返回原始文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对一堆文件(书)进行了过滤+分组操作.分组将仅返回共享相同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操作所得到的内容).有没有办法消除聚合管道中的冗余_idbooks部分?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆