Según su pregunta quiere obtener documentos del día actual de una colección mongodb . En mongoDB Shell cuando escribe new Date()
le da la fecha actual con la hora y este valor siempre varía cuando ejecuta la misma new Date()
así que probablemente su consulta sea así:
db.collectionName.find({"start_date":new Date()}).pretty()
Pero, creo que esta consulta devuelve los documentos que se presentarán en su colección pero con la misma Date
actual el valor puede no estar presente en sus documentos, por lo que este caso debe usar lo siguiente
db.collectionName.find({"start_date":{"$lte":new Date()}}).pretty()
O
db.collectionName.find({"start_date":{"$gte":new Date()}}).pretty()
En algunos casos, si desea encontrar una coincidencia exacta con year,month,day
entonces debería usar agregación
con $year,$month,$dayOfMonth en $project
así:
db.collectionName.aggregate({
"$project": {
"year": {
"$year": "$date"
},
"month": {
"$month": "$date"
},
"day": {
"$dayOfMonth": "$date"
}
}
}, {
"$match": {
"year": new Date().getFullYear(),
"month": new Date().getMonth() + 1, //because January starts with 0
"day": new Date().getDate()
}
})
En la consulta de agregación anterior, se devolverán los documentos que coincidan con la fecha actual, como year,month,day
de fecha actual. También reemplazas $match
como
var currentDate = new Date()
{
"$match": {
"year": currentDate.getFullYear(),
"month": currentDate.getMonth() + 1, //because January starts with 0
"day": currentDate.getDate()
}
}