Mongodb aggregate$count

0

的问题

我想算文件的数量返回的聚集。

我确信我最初聚集工作,因为我使用它后来在我的节目. 要做到所以我创造了一个管道变量(这里所谓的pipelineTest,问我,如果你想要看到详细的说明,但这是相当长期的,这就是为什么我不给线这里)。

算文件的数量返回时,我把我的管道: {$count: "totalCount"}

现在我想获得(或日志) totalCount 值。 我应该怎么做?

这里的聚合:

pipelineTest.push({$count: "totalCount"});
        cursorTest = collection.aggregate(pipelineTest, options)
        console.log(cursorTest.?)

感谢您的帮助,我读了很多,很多医生关于聚集和我还是不知道如何阅读结果的一个聚合...

aggregate mongodb
2021-11-23 20:45:58
2

最好的答案

0
  1. 假设你正在使用异步/等待法的-你需要等待结果的聚集。
  2. 你可以转换的标阵列,获得第一个元件阵列,并访问totalCount.
     pipelineTest.push({$count: "totalCount"});   
     cursorTest = await collection.aggregate(pipelineTest, options).toArray();  
     console.log(cursorTest[0].totalCount);
2021-11-23 21:05:41

谢谢你的回答。 嗯,如果我使用的等待,它返回这个错误:``cursorTest=等待收集。aggregate(pipelineTest,options)。toArray();^^^^^语法错误:等待着只有在异功能``
Aux98

如果我不用等待着,我得到了这个错误:``(节点:21748)UnhandledPromiseRejectionWarning:类型错误:无法读取财产'totalCount'的定义(节点:21748)UnhandledPromiseRejectionWarning:未经处理的承诺拒绝。 这个错误的起源或者通过投掷内部的异功能没有抓住块,或拒绝承诺不处理。抓(). 终止的节点上的进程未处理的承诺拒绝``
Aux98

@Aux98你可以试试这个? 收集。aggregate(pipelineTest,options)。toArray().然后(res=>的控制台。日志(res[0])).抓(err=>的控制台。错误(err))
Sergey Shevchuk

是的,它是工作那样,感谢你的帮助! 虽然.toArray是真的缓慢(需要5分钟后返回的结果)。 因为我需要来使用该功能6时候,它可能是一个问题。 但我会尽量优化我的代码,以便减少时间。 再次感谢
Aux98
0

聚集

db.mycollection.aggregate([
    {
        $count: "totalCount"
    }
])

结果,

[ { totalCount: 3 } ]

你的资料

试试下面的:

pipelineTest.push({$count: "totalCount"});
cursorTest = collection.aggregate(pipelineTest, options)
console.log(cursorTest.totalCount)
2021-11-23 20:58:33

谢谢你的回答,控制台返回: undefined
Aux98

但事实上它就是我想在第一次...
Aux98

啊,你必须使用一种特定的语言-也许JavaScript...
barrypicker

Ho是的,我忘了告诉那个,事实上它是javascript。
Aux98

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................