Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB find Query comparision with CurrentDate

Tags:

mongodb

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 } 
}
like image 629
Saikumar A Avatar asked Jun 11 '15 05:06

Saikumar A


People also ask

How do I find the difference between two dates in MongoDB?

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 .

How do I use $in in MongoDB?

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.

What is $$ now MongoDB?

$$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.


2 Answers

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()
  }
}
like image 148
Yogesh Avatar answered Sep 18 '22 14:09

Yogesh


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") });
like image 42
Dipesh Parmar Avatar answered Sep 18 '22 14:09

Dipesh Parmar