mongoose - MongoDB query for today call list -


this data of 1 user in user table in mongodb

{ "_id" : objectid("553799187174b8c402151d06"), "modified" : isodate("2015-04-22t12:50:32.477z"), "name" : "suresh", "userid" : "sursha6398", "created" : isodate("2015-04-22t12:50:32.457z"), "deleted" : false, "call_schedule" : [      {         "calltype" : "a",         "calldate" : isodate("2015-01-14t18:30:00.000z"),         "_id" : objectid("553799187174b8c402151d0e")     },      {         "calltype" : "a",         "calldate" : isodate("2015-01-31t18:30:00.000z"),         "_id" : objectid("553799187174b8c402151d0d")     },      {         "calltype" : "b",         "calldate" : isodate("2015-02-19t18:30:00.000z"),         "_id" : objectid("553799187174b8c402151d0c")     },      {         "calltype" : "b",         "calldate" : isodate("2015-03-02t18:30:00.000z"),         "_id" : objectid("553799187174b8c402151d0b")     },      {         "calltype" : "c",         "calldate" : isodate("2015-03-17t18:30:00.000z"),         "_id" : objectid("553799187174b8c402151d0a")     },      {         "calltype" : "b",         "calldate" : isodate("2015-03-30t18:30:00.000z"),         "_id" : objectid("553799187174b8c402151d09")     },      {         "calltype" : "a",         "calldate" : isodate("2015-04-08t18:30:00.000z"),         "_id" : objectid("553799187174b8c402151d08")     },      {         "calltype" : "d",         "calldate" : isodate("2015-04-22t18:30:00.000z"),         "_id" : objectid("553799187174b8c402151d07")     } ], "__v" : 0 

}

there hundreds of users , each have 1 or more call scheduled. need find total number of call scheduled today corresponding userid using mongodb query.

to total number of calls scheduled corresponding userid, need use mongodb's aggregation framework. aggregation pipeline consists of $match pipeline stage first step filters documents in collection have call schedule between start of today , before tomorrow i.e. query criteria date range. next pipeline stage uses $unwind operator deconstruct call_schedule array can output of document each element in array. each output document replaces array element value. after $unwind operation filter documents further meet $match criteria. next pipeline stage $group operator can group documents userid , calculate aggregated sum using $sum operator on each document in group. final aggregation pipeline this:

var start = new date(); // today's date var end = new date(new date().setdate(new date().getdate()+1));  var pipeline = [     {         "$match": {             "call_schedule.calldate": {                 "$gte": start,                 "$lt": end             }         }     },     {         "$unwind": "$call_schedule"     },     {         "$match": {             "call_schedule.calldate": {                 "$gte": start,                 "$lt": end             }         }     },     {         "$group": {             "_id": {                 "userid": "$userid"             },             "total": {                 "$sum": 1             }         }     }   ];  db.collection.aggregate(pipeline); 

output:

/* 0 */ {     "result" : [          {             "_id" : {                 "userid" : "sursha6398"             },             "total" : 3 // <-- gives total number of calls made userid today         }     ],     "ok" : 1 } 

-- update --

to filtered call schedule list, modify $group pipeline stage include call_schedule list using $addtoset method:

var today = new date(); var tomorrow = new date(new date().setdate(new date().getdate()+1));  db.collection.aggregate([     {         "$match": {             "call_schedule.calldate": {                 "$gte": today,                 "$lt": tomorrow             }         }     },     {         "$unwind": "$call_schedule"     },     {         "$match": {             "call_schedule.calldate": {                 "$gte": today,                 "$lt": tomorrow             }         }     },     {         "$group": {             "_id": {                 "userid": "$userid"             },             "call_list": {                 "$addtoset": "$call_schedule"             }         }     }   ]); 

with sample collection has following document (that include 3 call schedules made today's date i.e. 2015-04-29:

/* 0 */ {     "_id" : objectid("553799187174b8c402151d06"),     "modified" : isodate("2015-04-22t12:50:31.477z"),     "name" : "suresh",     "userid" : "sursha6398",     "created" : isodate("2015-04-22t12:50:32.457z"),     "deleted" : false,     "call_schedule" : [          {             "calltype" : "a",             "calldate" : isodate("2015-01-14t18:30:00.000z"),             "_id" : objectid("553799187174b8c402151d0e")         },          {             "calltype" : "a",             "calldate" : isodate("2015-01-31t18:30:00.000z"),             "_id" : objectid("553799187174b8c402151d0d")         },          {             "calltype" : "b",             "calldate" : isodate("2015-02-19t18:30:00.000z"),             "_id" : objectid("553799187174b8c402151d0c")         },          {             "calltype" : "b",             "calldate" : isodate("2015-03-02t18:30:00.000z"),             "_id" : objectid("553799187174b8c402151d0b")         },          {             "calltype" : "c",             "calldate" : isodate("2015-03-17t18:30:00.000z"),             "_id" : objectid("553799187174b8c402151d0a")         },          {             "calltype" : "b",             "calldate" : isodate("2015-04-29t18:30:00.000z"),             "_id" : objectid("553799187174b8c402151d09")         },          {             "calltype" : "a",             "calldate" : isodate("2015-04-29t18:30:00.000z"),             "_id" : objectid("553799187174b8c402151d08")         },          {             "calltype" : "d",             "calldate" : isodate("2015-04-29t18:30:00.000z"),             "_id" : objectid("553799187174b8c402151d07")         }     ] } 

the above aggregation give output:

/* 0 */ {     "result" : [          {             "_id" : {                 "userid" : "sursha6398"             },             "call_list" : [                  {                     "calltype" : "d",                     "calldate" : isodate("2015-04-29t18:30:00.000z"),                     "_id" : objectid("553799187174b8c402151d07")                 },                  {                     "calltype" : "a",                     "calldate" : isodate("2015-04-29t18:30:00.000z"),                     "_id" : objectid("553799187174b8c402151d08")                 },                  {                     "calltype" : "b",                     "calldate" : isodate("2015-04-29t18:30:00.000z"),                     "_id" : objectid("553799187174b8c402151d09")                 }             ]         }     ],     "ok" : 1 } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -