Reputation: 29
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" }
Below is my query but unable to find exitState in all documents . Can you please?
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"]); }); });
Upvotes: 0
Views: 414
Reputation: 1
I can't understand what is your question exactly. if you want to know how many docs exist in the collection and count them by their exitState, this function retuns what you want. I don't know $match works like this or not, But please log the result for test before doing any action on it.
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} ]
});
Upvotes: 0
Reputation: 51
db.tablename.find({},{"exitStates":1}).count()
https://www.w3resource.com/mongodb-exercises/mongodb-exercise-4.php
Upvotes: 0
Reputation: 1562
Maybe you can filter the results:
const result5 = result4.filter((result) => result.exitState && result.exitState !== '');
const nbResults = result5.length;
Upvotes: 0