Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find data for specific date in mongodb

I wanted to find data for 2014-08-01 from MongoDB collection. Date in MongoDB is in ISO format.I wrote below query, but it gives me very huge number which i suspect is not the data for 1 day. Can some one let me know what is wrong with query. sd is the key

db.history.count({sd:{$gte: new Date("2014-08-01")},sd:{$lt:new Date("2014-08-01")}})
like image 657
Neil Avatar asked Oct 28 '25 06:10

Neil


1 Answers

Ranges can be specified using $gte and $lt in the same document portion:

db.history.count({ "sd": {
    "$gte": new Date("2014-08-01"), "$lt": new Date("2014-08-02")
}})

Otherwise the argument are considered to be separate and a logical "and" condition. But really since you are using the same "key" in your "sd" field, this is not allowed in a JSON/BSON document and violates the "unique" keys rule for hash structures in general. So one condition overwrites the other and only one is applied.

That is why your result is wrong. Use as shown above instead.

like image 168
Neil Lunn Avatar answered Oct 30 '25 00:10

Neil Lunn