You can start the aggregate operation using the aggregate() function, which accepts $redact , which uses the date aggregation operators in the condition expression:
var month = 11; db.collection.aggregate([ { "$redact": { "$cond": [ { "$eq": [ { "$month": "$createdAt" }, month ] }, "$$KEEP", "$$PRUNE" ] } } ])
For another request
var day = 17; db.collection.aggregate([ { "$redact": { "$cond": [ { "$eq": [ { "$dayOfMonth": "$createdAt" }, day ] }, "$$KEEP", "$$PRUNE" ] } } ])
Using OR
var month = 11, day = 17; db.collection.aggregate([ { "$redact": { "$cond": [ { "$or": [ { "$eq": [ { "$month": "$createdAt" }, month ] }, { "$eq": [ { "$dayOfMonth": "$createdAt" }, day ] } ] }, "$$KEEP", "$$PRUNE" ] } } ])
Using AND
var month = 11, day = 17; db.collection.aggregate([ { "$redact": { "$cond": [ { "$and": [ { "$eq": [ { "$month": "$createdAt" }, month ] }, { "$eq": [ { "$dayOfMonth": "$createdAt" }, day ] } ] }, "$$KEEP", "$$PRUNE" ] } } ])
The $redact includes the functionality of the $project and $match pipeline and will return all documents matching the condition using $$KEEP and discard those that are not using the $$PRUNE variable.
chridam Feb 20 '17 at 12:06 on 2017-02-20 12:06
source share