使用"$ count"在"addField"中, MongoDB聚合中的操作 [英] Using "$count" Within an "addField" Operation in MongoDB Aggregation
问题描述
我试图找到聚合运算符的正确组合,以将标题为"totalCount"的字段添加到我的mongoDB视图中.
这将使我获得聚合管道此特定阶段的计数,并根据每个文档的计数结果将其输出:
{
$count: "count"
}
但是我最终得到的结果是一个文档,而不是我要尝试完成的结果,那就是将该值打印为addedField
,它是所有文档中的字段/值,甚至更好的是,该值将in addition
打印到返回的文档中.
我已经尝试过了,但是它给我一个错误"无法识别的表达式'$ count',":
{
$addFields: {
"totalCount" : { $count: "totalCount" }
}
}
为此正确的语法构造是什么?是否可以通过这种方式执行此操作,或者我是否需要使用$sum
或其他一些运算符来进行此操作?我也尝试过这个:
{
$addFields: {
"totalCount" : { $sum: { _id: 1 } }
}
},
...但是它并没有给我任何错误,它只是在每个文档上打印0
作为该字段的值,而不是所有文档的总数.
总计数始终是一个文档的结果,因此您需要 在 然后,您必须使用 $ unwind 来将数组转换为多个文档,并使用 $ replaceRoot href ="https://docs.mongodb.com/manual/reference/operator/aggregation/mergeObjects/index.html" rel ="noreferrer"> $ mergeObjects $facet
阶段之后,您将获得像这样的单个文档{
"totalCount" : [
{
"value" : 3
}
],
"pipelineResults" : [
{
"_id" : ObjectId("5b313241120e4bc08ce87e46")
},
//....
]
}
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屋!