如何计算在MongoDB中为某个字段找到的值的数量?

时间:2018-09-25 10:07:26

标签: node.js mongodb mongodb-query

I have to find "exitState" : this is single document , if multiple documents how to find.
    { 
        "_id" : "abc", 
        "exitType" : "Hang", 
        "exitState" : "INDIA", 
        "outcome" : "Successful", 
        "CEV" : [
            {
                "LogID" : "CEV", 
                "ReportingMode" : "N", 
                "Log_DateTime" : "02:23:2016 00:17:48:913", 
                "Log_TS" : NumberLong(1456186668913), 
                "ServiceType" : "TEL", 
                "MsgID" : "25000", 
                "SysName" : "test123", 
                "ProcessID" : "9611", 
                "Port" : "0", 
                "ModuleName" : "ArcCDR::CDR_CustomEvent", 
                "AppName" : "testVXML2", 
                "MsgTxt" : "abc::24::Test::outcome=Successful$$$exitType=Hang$$$exitState=INDIA", 
                "Record_Key" : "abc", 
                "Token1" : "24", 
                "CustomerName" : "Test", 
                "CEV_MsgTxt" : "outcome=Successful$$$exitType=Hang$$$exitState=INDIA", 
                "outcome" : "Successful", 
                "exitType" : "Hang", 
                "exitState" : "INDIA"
            }
        ], 
        "language" : "ENGLISH", 
        "SC_TS" : ISODate("2016-02-23T00:17:06.060+0000"), 
        "SC_TimeMS" : NumberLong(1456186626060), 
        "CDR_SC" : {
            "LogID" : "CDR", 
            "ReportingMode" : "N", 
            "Log_DateTime" : "02:23:2016 00:17:06:060", 
            "Log_TS" : NumberLong(1456186626060), 
            "ServiceType" : "TEL", 
            "MsgID" : "20010", 
            "SysName" : "test123", 
            "ProcessID" : "9611", 
            "Port" : "0", 
            "ModuleName" : "TEL_AnswerCall", 
            "AppName" : "testVXML2", 
            "MsgTxt" : "abc:SC:testVXML2:452:607856:0223201600170606::", 
            "Record_Key" : "abc", 
            "CDR_Type" : "SC", 
            "Token2" : "testVXML2", 
            "Token3" : "452", 
            "Token4" : "607856", 
            "Token5" : "0223201600170606"
        }, 
        " SC_TS_TZ" : ISODate("2016-02-23T00:17:06.060+0000"), 
        "EC_TS" : ISODate("2016-02-23T00:17:48.910+0000"), 
        "EC_TS_TZ" : ISODate("2016-02-23T00:17:48.910+0000"), 
        "EC_TimeMS" : NumberLong(1456186668910), 
        "CDR_EC" : {
            "LogID" : "CDR", 
            "ReportingMode" : "N", 
            "Log_DateTime" : "02:23:2016 00:17:48:910", 
            "Log_TS" : NumberLong(1456186668910), 
            "ServiceType" : "TEL", 
            "MsgID" : "20011", 
            "SysName" : "test123", 
            "ProcessID" : "9611", 
            "Port" : "0", 
            "ModuleName" : "TEL_SRRecognizeV2", 
            "AppName" : "testVXML2", 
            "MsgTxt" : "abc:EC:02:0223201600174891::", 
            "Record_Key" : "abc", 
            "CDR_Type" : "EC", 
            "Token2" : "02", 
            "Token3" : "0223201600174891"
        }, 
        "CustomerName" : "Test"
    }

以下是我的查询,但无法在所有文档中找到exitState。可以吗?

dbo.ProductModel.aggregate([
          {$match: {"EC_TS":{$gte:new Date(start.toISOString()), $lte:new Date(end.toISOString())}} },
         {$group:
            {_id: '$exitState', count : {$sum: 1} }
          }
      ]).toArray(function(err, result4) {
                console.log(+ result4[0]["exitState"]);
                console.log("Total exitState=" + result4[0]["total"]);
        q4result=(result4[0]["total"]);
                     });

});

3 个答案:

答案 0 :(得分:0)

也许您可以过滤结果:

const result5 = result4.filter((result) => result.exitState && result.exitState !== '');
const nbResults = result5.length;

答案 1 :(得分:0)

db.tablename.find({},{“ exitStates”:1})。count()

https://www.w3resource.com/mongodb-exercises/mongodb-exercise-4.php

答案 2 :(得分:0)

我不明白您到底要问什么。如果您想知道集合中存在多少个文档,并按它们的exitState进行计数,则此函数会重新调整您想要的内容。我不知道$ match是否会这样工作,但是请在对结果进行任何操作之前记录结果以进行测试。

LegendText