You could run an aggregate operation with the aggregate() function that takes in a $redact pipeline that uses the Date Aggregation Operators in the condition expression:
var month = 11;
db.collection.aggregate([
{
"$redact": {
"$cond": [
{ "$eq": [ { "$month": "$createdAt" }, month ] },
"$$KEEP",
"$$PRUNE"
]
}
}
])
For the other 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 operator incorporates the functionality of $project and $match pipeline and will return all documents match the condition using $$KEEP and discard from the pipeline those that don't match using the $$PRUNE variable.