dayofweek - How to find documents in all mondays from last 7 weeks in mongodb -
{ "_id" : objectid("568b650543712795bf864a45") "companyid" : "55e2d7cfdc8f74d14f5c900f", "timestamp" : isodate("2014-12-02t18:30:00.000z") }, { "_id" : objectid("568b650543712795bf864a46") "companyid" : "55e2d7cfdc8f74d14f5c900f", "timestamp" : isodate("2014-12-03t18:30:00.000z") }, { "_id" : objectid("568b650543712795bf864a47") "companyid" : "55e2d7cfdc8f74d14f5c900f", "timestamp" : isodate("2014-12-04t18:30:00.000z") } retrieve documents in mondays timestamp field last 7 weeks.
you have use mongodb aggregation framework achieve this.
find date of start (current day - 7 weeks) in whatever programming language using. have use aggregation operation $dayofweek achieve this
var pipeline = [ { $match: {timestamp: {$gte: startdate}} }, { $project: {dayofweek: {$dayofweek: '$timestamp'}} }, { $match: {dayofweek: 1} } ]; db.mycollection.aggreage(pipeline) in above have projected 1 field, may project more fields.
for more information please click $dayofweek
Comments
Post a Comment