Mongo Aggregate query using date part of ISODate.

Most of the time we only need date part of  iso date in any document during an aggregation. Here is how to get the date part for aggregation,

  1. Project ( $project ) as a new filed, say “considered_date”
  2. Group with the field

Example

db.collection.aggregate([
{$match : {  any filtering with $match }}
,{$project : { _id :1 , any project fileds
, “considered_date” :      {“$concat” : [
{“$substr” : [{“$dayOfMonth” : “$date_1”}, 0, 2]}, “-“,
{“$substr” : [{“$month” : “$date_1”}, 0, 2]}, “-“,
{“$substr” : [{“$year” : “$date_1”}, 0, 4]}
]} } }
,{$lookup : { any lookups}}
//,any other filtering
,{$group : { _id : { “filed 1″:”$field1”, “date”:”$considered_date” }, “count”:{$sum:1} }}
])

Advertisements

About buminda

buminda.com
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s