【问题标题】:How to count the number of values found for a field in MongoDB?如何计算在 MongoDB 中为某个字段找到的值的数量?
【发布时间】:2019-02-28 22:12:17
【问题描述】:
I have to find "exitState" : this is single document , if multiple documents how to find.
{ “_id”:“abc”, "exitType" : "挂起", “exitState”:“印度”, “结果”:“成功”, “CEV”:[ { "LogID" : "CEV", “报告模式”:“N”, "Log_DateTime" : "02:23:2016 00:17:48:913", "Log_TS" : NumberLong(1456186668913), “服务类型”:“电话”, "MsgID" : "25000", “系统名称”:“test123”, “进程ID”:“9611”, “端口”:“0”, "ModuleName" : "ArcCDR::CDR_CustomEvent", "AppName" : "testVXML2", "MsgTxt" : "abc::24::Test::outcome=Successful$$$exitType=Hang$$$exitState=INDIA", "Record_Key" : "abc", “令牌1”:“24”, “客户名称”:“测试”, "CEV_MsgTxt" : "结果=成功$$$exitType=Hang$$$exitState=INDIA", “结果”:“成功”, "exitType" : "挂起", “exitState”:“印度” } ], “英语语言”, "SC_TS" : ISODate("2016-02-23T00:17:06.060+0000"), "SC_TimeMS" : NumberLong(1456186626060), “CDR_SC”:{ "LogID" : "CDR", “报告模式”:“N”, "Log_DateTime" : "02:23:2016 00:17:06:060", "Log_TS" : NumberLong(1456186626060), “服务类型”:“电话”, "MsgID" : "20010", “系统名称”:“test123”, “进程ID”:“9611”, “端口”:“0”, "ModuleName" : "TEL_AnswerCall", "AppName" : "testVXML2", "MsgTxt" : "abc:SC:testVXML2:452:607856:0223201600170606::", "Record_Key" : "abc", “CDR_Type”:“SC”, "Token2" : "testVXML2", “令牌3”:“452”, “令牌4”:“607856”, “令牌5”:“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", “报告模式”:“N”, "Log_DateTime" : "02:23:2016 00:17:48:910", "Log_TS" : NumberLong(1456186668910), “服务类型”:“电话”, "MsgID" : "20011", “系统名称”:“test123”, “进程ID”:“9611”, “端口”:“0”, “模块名称”:“TEL_SRRecognizeV2”, "AppName" : "testVXML2", "MsgTxt" : "abc:EC:02:0223201600174891::", "Record_Key" : "abc", “CDR_Type”:“EC”, “令牌2”:“02”, “令牌3”:“0223201600174891” }, “客户名称”:“测试” }

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

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

【问题讨论】:

  • 使用$exists 运算符。
  • 能否请您在此处 ping 查询..
  • dbo.ProductModel.find({"EC_TS":{$gte:new Date(start.toISOString()), $lte:new Date(end.toISOString())}, exitState: { "$exists": true }})
  • 还是一样没有找到超过元素
  • 然后去掉$lte $gte 在所有文档中查找? dbo.ProductModel.find({ exitState: { "$exists": true }})

标签: node.js mongodb mongodb-query


【解决方案1】:

也许你可以过滤结果:

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

【讨论】:

  • 不明白你能写完整的查询吗?
  • 这不是查询的一部分。我的 sn-p 应该在 toArray() 回调中。它过滤具有exitState 属性的结果,然后对过滤结果进行计数。
【解决方案2】:

db.tablename.find({},{"exitStates":1}).count()

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

【讨论】:

  • 谢谢,但我必须打印所有 ExitState 及以上查询不适合我。
【解决方案3】:

我不明白你的问题到底是什么。如果您想知道集合中存在多少个文档并通过它们的 exitState 对它们进行计数,此函数将返回您想要的内容。我不知道 $match 是否这样工作,但请在对其执行任何操作之前记录结果以进行测试。

dbo.ProductModel.aggregate([
    { $match: { "EC_TS": { $gte: new Date( start.toISOString() ),
        $lte: new Date( end.toISOString() ) } } },
    { $group: {_id: '$exitState', count : {$sum: 1} } }
], (err, result) => {
    if (err) throw err;
    console.log(result);
    // result is like this:
    // [ {"_id": "INDIA", "count": 3}, {"_id": "US", "count": 8} ]
});

【讨论】:

    猜你喜欢
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多