使用“$count"在“addField"中MongoDB聚合中的操作 [英] Using "$count" Within an "addField" Operation in MongoDB Aggregation

查看:28
本文介绍了使用“$count"在“addField"中MongoDB聚合中的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到聚合运算符的正确组合,以将标题为totalCount"的字段添加到我的 mongoDB 视图中.

这将为我提供聚合管道此特定阶段的计数,并将其作为对每个文档的计数结果输出:

<代码> {$count:计数"}

但是我最终得到了一个带有这个结果的文档,而不是我想要完成的事情,即让这个值打印为一个 addedField ,它是一个字段/值所有的文档,或者甚至更好的是,一个在返回的文档中打印 addition 的值.

我已经试过了,但它给了我一个错误"无法识别的表达式 '$count'",":

<代码> {$addFields:{"totalCount" : { $count: "totalCount" }}}

正确的句法结构是什么?是否可以这样做,或者我是否需要使用 $sum 或其他一些运算符来完成这项工作?我也试过这个:

<代码> {$addFields:{"totalCount" : { $sum: { _id: 1 } }}},

...但虽然它没有给我任何错误,但它只是将 0 打印为每个文档上该字段的值,而不是所有文档的总数.

解决方案

Total count 将始终是一个文档结果,因此您需要 $facet 运行多个聚合管道,然后合并结果.假设您的常规管道包含简单的 $project,并且您希望将其结果与 $count 合并.您可以在聚合下运行:

db.col.aggregate([{$facet:{总数: [{ $count: "值" }],管道结果:[{$project: { _id: 1 }//这里是你的常规聚合管道}]}},{$unwind: "$pipelineResults"},{$unwind: "$totalCount"},{$替换根:{新根:{$mergeObjects: [ "$pipelineResults", { totalCount: "$totalCount.value" } ]}}}])

$facet 阶段之后你会得到这样的单个文档

<代码>{总数" : [{价值":3}],管道结果":[{"_id" : ObjectId("5b313241120e4bc08ce87e46")},//....]}

那么你必须使用 $unwind 来将数组转换为多个文档和 $replaceRoot$mergeObjects 将常规管道结果提升到根级别.>

I am trying to find the correct combination of aggregation operators to add a field titled "totalCount" to my mongoDB view.

This will get me the count at this particular stage of the aggregation pipeline and output this as the result of a count on each of the documents:

    {
        $count: "count"
    }

But I then end up with one document with this result, rather than what I'm trying to accomplish, which is to make this value print out as an addedField that is a field/value on all of the documents, or even better, a value that prints in addition to the returned documents.

I've tried this but it gives me an error ""Unrecognized expression '$count'",":

    {
        $addFields: {
          "totalCount" : { $count: "totalCount" }
        }
    }

What would the correct syntactical construction be for this? Is it possible to do it this way, or do I need to use $sum, or some other operator to make this work? I also tried this:

    {
        $addFields: {
          "totalCount" : { $sum: { _id: 1 } }
        }
    },

... but while it doesn't give me any errors, it just prints 0 as the value for that field on every document rather than the total count of all documents.

解决方案

Total count will always be a one-document result so you need $facet to run mutliple aggregation pipelines and then merge results. Let's say your regular pipeline contains simple $project and you want to merge it's results with $count. You can run below aggregation:

db.col.aggregate([
    {
        $facet: {
            totalCount: [
                { $count: "value" }
            ],
            pipelineResults: [
                {
                    $project: { _id: 1 } // your regular aggregation pipeline here 
                }
            ]
        }
    },
    {
        $unwind: "$pipelineResults"
    },
    {
        $unwind: "$totalCount"
    },
    {
        $replaceRoot: {
            newRoot: {
                $mergeObjects: [ "$pipelineResults", { totalCount: "$totalCount.value" } ]
            }
        }
    }
])

After $facet stage you'll get single document like this

{
    "totalCount" : [
        {
            "value" : 3
        }
    ],
    "pipelineResults" : [
        {
            "_id" : ObjectId("5b313241120e4bc08ce87e46")
        },
        //....
    ]
}

Then you have to use $unwind to transform arrays into multiple documents and $replaceRoot with $mergeObjects to promote regular pipeline results into root level.

这篇关于使用“$count"在“addField"中MongoDB聚合中的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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