from the mongodb shell, I'm having an issue with a date query. This is a test database and the problem is better explained by example:
Example field in a collection
"createdOn" : ISODate("2015-11-23T00:49:01.800Z")
When I enter a date of this month for testing at the shell such as:
new Date(2015, 11, 23)
ISODate("2015-12-23T05:00:00Z")
You can see it's a month a head, meaning It's messing my queries. Where I have to drop back a month in order to get what I need for this month:
db.collection.find({'createdOn': {'$lte': new Date(2015, 10, 23)}}).count()
306
db.collection.find({'createdOn': {'$gte': new Date(2015, 10, 23)}}).count()
10
All entries above were created in Nov, but when using the correct month:
db.collection.find({'createdOn': {'$gte': new Date(2015, 11, 23)}}).count()
0
How does this even happen?
You should use Date like this: new Date("2015-11-23"). See Mongo Reference
What you are doing is almost right but you have missed on a point that:
Javascript counts month from 0 to 11 instead of 1 to 12
January is 0. December is 11. So to see for entries created in november you have to apply query like:
db.collection.find({'createdOn': {'$gte': new Date(2015, 10, 23)}})
Related
I am having a MongoDb collection of 70M items (200GB) and I am trying to get those in the range [from_date, to_date] using the command:
from_date = datetime.datetime(2015, 1, 14, 9, 46, 23)
to_date = datetime.datetime(2015, 1, 14, 9, 46, 24)
db.collection.find({"datetime": {"$gte": from_date, "$lte": to_date}})
However it takes a lot of time to retrieve those items even for a single query. Is there any more efficient way to do this?
I have been trying to filter all the objects which are created within a specific date range as well a specific time range.
For example: Consider the following scenario
if following are my filter's start date, end date, start time and end time.
startDate: 23/01/2015 endDate: 25/01/2015
startTime: 10:00:00 endTime: 16:00:00
So I need to filter all the objects which are created between the startDate(23/01/2015) and endDate(25/01/2015). Also the creation time should be between startTime(10:00:00) and endTime(16:00:00).
So an object which is created on 24/01/2015 but at 09:00:00 will not be filtered according to my use case.
Is there any mongo query to achieve the same where we can pass date and time separately. If not kindly suggest some workaround.
for (var d = new Date(2015, 0, 23, 10, 00, 00); d <= new Date(2015, 0,25,16,00,00); d.setDate(d.getDate() + 1)) {
var d1 = d.setHours(d.getHours()+6);
db.coll.find({createdDate:{$gte:d, $lte:d1}})
}
I am trying to find all documents with date field within date range as shown below in my Meteor app, but for some reason it is always returning empty records, can someone please tell me what I might be doing wrong / missing here? Thanks
Sample document:
_id: "tMSfNq9JR85XDaQe5"customerid: "QDGvBQhS6vYgZtnRr"date: Sun Dec 07 2014 19:50:21 GMT+0800 (HKT)description: "Test"
Tried using queries from Chrome console as follows:
Custlog.find({date: {$gt: new Date(2014, 12, 1) , $lt: new Date(2014, 12, 10) }}).fetch()
Custlog.find({date: {$gt: new Date(01/12/2014) , $lt: new Date(10/12/2014) }}).fetch()
In their infinite wisdom, the implementors of the JavaScript Date Object made the month field zero indexed. So, January 1 is new Date(2014, 0, 1) and December 1 is new Date(2014, 11, 1). What you have Date(2014, 12, 1) is actually January 1, 2015.
Similarly, new Date(01/12/2014) is equivalent to dividing 1 by 12 and then dividing that by 2014 and then sending that as a number to the date object which is zero so it should give you the Unix Epoc, which in your time zone (GMT+08) is January 1, 1970.
On the mongo shell this returns a document just fine:
> db.orderbook_log.findOne({'time': { '$gte': new Date(2014, 9, 24, 17, 38, 20, 546000), '$lt': new Date(2014, 10, 24, 17, 39, 20, 546000)}})
//... returns document with this time stamp:
"time" : ISODate("2014-10-25T00:47:30.819Z")
Notice I used "9" for October because JavaScript's months are 0-11.
And I also tested with "23" as the day because it looks like JS days are also 0-indexed, and that also returned a document: "time" : ISODate("2014-10-24T17:32:13.595Z")
atime = datetime.datetime(2014, 10, 24, 17, 38, 20, 546000)
btime = datetime.datetime(2014, 10, 24, 17, 39, 20, 546000)
future_book = log.find_one({"time": {"$gte": atime, "$lt": btime}})
But when I execute find_one in pymongofuture_book is None
All I'm really trying to do is loop though the first 100 records or so and get a record that occurred a relative minute later.
Javascript days are not zero-indexed, alas. Only months are.
I see in your Javascript you're adding 546,000 ms to the first date, so that results in 2014-10-24 at 17:48:26. Javascript then converts to your local timezone, so in my case it adds 5 hours:
> new Date(2014, 10, 24, 17, 39, 20, 546000)
ISODate("2014-11-24T22:48:26Z")
This is then compared (ignoring timezones) with the "time" field in your documents.
Better to remove the final milliseconds argument, and use the MongoDB shell's ISODate function, which is designed to be more rational than Javascript Dates:
> ISODate('2014-10-24T17:38:20')
ISODate("2014-10-24T17:38:20Z")
That will then compare to your documents in the way you expect, and it should match the PyMongo behavior. I suggest you drop the milliseconds argument from your Python datetime constructors, too, to clarify things.
I save a mongodb field 'date' as datetime object
and use following queries
1.db.test_collection.find({'date': {'$gte': start_date_time}})
2. db.test_collection.find({"date": {"$gte": start_date_time, "$lte": end_date_time}
where start_date_time is (say)2014-06-03 00:00:00 and end_date_time 2014-06-03 23:59:59
i insert some values to mongodb, But I don't give any results.
please help me to solve the issue.
You should use the datetime type.
import datetime
db.test_collection.find({'date': {'$gte': datetime.datetime(2014, 6, 3, 16, 46) }})
You can see more in the documentation (http://api.mongodb.org/python/current/tutorial.html)
Hope it works fine for you!