I want to fetch current day documents from a MongoDB collection. My documents look like this:
{
"_id" : ObjectId("55743941789a9abe7f4af3fd"),
"msisdn" : "9xxxxxxxxxx",
"act_date" : ISODate("2014-11-24T00:00:00Z"),
"date" : ISODate("2015-06-07T00:00:00Z"),
"recharge" : { "recharge_amt" : 0, "rechargetype" : "WEB" },
"voice" : { "local_og_mou" : 20, "local_other_mobile_og_mou" : 0, "nld_og_mou" : 0, "nld_other_mobile_og_mou" : 10 },
"gprs" : { "total_access_count" : 1, "total_datavolume_mb" : 42 },
"sms" : { "freesms" : 3, "local_sms_count" : 0, "nat_sms_count" : 0, "inter_sms_count" : 0 }
}
The $dateDiff expression returns the integer difference between the startDate and endDate measured in the specified units . Durations are measured by counting the number of times a unit boundary is passed. For example, two dates that are 18 months apart would return 1 year difference instead of 1.5 years .
Use the $in Operator to Match Values This query selects all documents in the inventory collection where the value of the quantity field is either 5 or 15. Although you can write this query using the $or operator, use the $in operator rather than the $or operator when performing equality checks on the same field.
$$NOW is an aggregation variable, which means that it can by used only within aggregation pipeline. Due to the documentation, save() method is deprecated. use insertOne() instead. To insert current date you need to provide date value on the client side.
As per your question you want to fetch current day documents from a mongodb collection. In mongoDB shell when you type new Date()
it gives you current date with time and this value always vary when you run same new Date()
so probably your query like this :
db.collectionName.find({"start_date":new Date()}).pretty()
But, I think this query return the those documents which will presents in your collection but same current Date
value may be not presents in your documents SO this case you should use following
db.collectionName.find({"start_date":{"$lte":new Date()}}).pretty()
Or
db.collectionName.find({"start_date":{"$gte":new Date()}}).pretty()
In some case If you want to find exact match with year,month,day
then you should use aggregation with $year,$month,$dayOfMonth in $project like this :
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()
}
})
In above aggregation query will return those documents which match current date like year,month,day
of current date. Also you replace $match
as
var currentDate = new Date()
{
"$match": {
"year": currentDate.getFullYear(),
"month": currentDate.getMonth() + 1, //because January starts with 0
"day": currentDate.getDate()
}
}
You can do this using below query.
db.collection.find({"start_date" : { $gte : new ISODate("2015-05-27T00:00:00Z") }});
Note : above query will return documents which are greather than specified date.
To find a date that equals another date
db.collection.find({"start_date" : new ISODate("2015-05-27T00:00:00Z") });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With