16

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 } } 
6
  • Be more clear with your edit.... Commented Jun 11, 2015 at 5:37
  • docs.mongodb.org/manual/reference/operator/update/currentDate Commented Jun 11, 2015 at 5:44
  • add your document structure for more clarification. Commented Jun 11, 2015 at 5:44
  • Hi Dev,Below is the data structure... Commented Jun 11, 2015 at 5:59
  • { "_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 } } Commented Jun 11, 2015 at 6:03

5 Answers 5

25

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() } } 
Sign up to request clarification or add additional context in comments.

Comments

10

If you are using Mongoose you can include timeStamp: true after your schema definition to get autogenerated createdAt and updatedAt fields, Mongoose will take care of it.

const itemSchema = mongoose.Schema({ // Your Schema definition here }, { timestamps: true }) 

In your case, you need to compare your TimeStamp key with the start of today which is 12 AM with ISO string as 2019-11-08T00:00:00.000Z and end of the day which is 11:59 PM with ISO string as 2019-11-08T23:59:59.999Z.
The below code will do it for you.

let queryObj = {} const startOfDay = new Date(new Date().setUTCHours(0, 0, 0, 0)).toISOString() const endOfDay = new Date(new Date().setUTCHours(23, 59, 59, 999)).toISOString() queryObj.createdAt = { $gte: startOfDay, // 2019-11-08T00:00:00.000Z $lt: endOfDay // 2019-11-08T23:59:59.999Z } let items = item.find(obj) // new Date().setUTCHours(0, 0, 0, 0) Generates this weird string '1573171200000' which is not a human readable format of TimeStamp // To convert it into ISO String we have .toISOString() method 

2 Comments

This uses mongoose. The OP said nothing about using mongoose. MongoDB will not "take care of it".
My bad @DanDascalescu , updated the answer. Thanks
3

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") }); 

Comments

0

You can do a simpler expression match as below:

$match: { $expr: {$eq: [{$year: '$start_date'}, {$year: new Date()}]}, $expr: {$eq: [{$month: '$start_date'}, {$month: new Date()}]}, $expr: {$eq: [{$day: '$start_date'}, {$day: new Date()}]}, } 

Comments

0

With MongoDB v5.0+, you can perform date-only comparison(without time) by using $dateTrunc. Compare with $$NOW to get documents in the current date.

db.collection.aggregate([ { "$match": { $expr: { $eq: [ { $dateTrunc: { date: "$startDate", unit: "day" } }, { $dateTrunc: { date: "$$NOW", unit: "day" } } ] } } } ]) 

Mongo Playground

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.