Use $lte for datetime querying is not working - mongodb

I want to query data by using datetime that less than 15:00 in 2016-01-14 but it is not working.
Here is my example collection
{
"_id" : ObjectId("5697528237b79c198c94ad1a"),
"actionName" : "touchMove",
"timeStamp" : ISODate("2016-01-14T14:47:13.596Z")
}
{
"_id" : ObjectId("5697528237b79c198c94ad16"),
"actionName" : "touchDown",
"timeStamp" : ISODate("2016-01-14T14:47:13.597Z")
}
{
"_id" : ObjectId("5697528237b79c198c94ad1e"),
"actionName" : "touchMove",
"timeStamp" : ISODate("2016-01-14T16:01:49.620Z")
}
{
"_id" : ObjectId("5697528237b79c198c94ad1b"),
"actionName" : "touchDown",
"timeStamp" : ISODate("2016-01-14T16:01:50.010Z")
}
{
"_id" : ObjectId("5697528237b79c198c94ad17"),
"actionName" : "touchMove",
"timeStamp" : ISODate("2016-01-14T16:01:49.630Z")
}
And here is my query code
db.getCollection('app1_touchpoint').find({
'timeStamp' : {'$lte':new Date(2016, 01, 14, 15, 00)}
})
When I do query, the data that have a 'timeStamp' more than 2016-01-14 15:00 are shown in the result too. Actually all data are shown.
Is anyone able to give me some advice as to how I should do this query in the right way? Thank you.

The date you are adding to your query is a local date by default in c#. But - dates in mongo are utc by default.
You have a couple of options.
Move to a place that's on the other side of the utc line. This will solve your problem for lte, but may create a problem for gte.
Use DateTimeKind.Utc and set that date type to utc before you use it to query.
For Example
If I were going to do that with your sample data, I'd do it this way:
var dt = DateTime.SpecifyKind(new Date(2016, 01, 14, 15, 0, 0), DateTimeKind.Unspecified);
db.getCollection('app1_touchpoint').find({
'timeStamp' : {'$lte':dt}
});
I assume the goal is to query the dates in your db as they sit (not to query them AFTER you convert those dates to your timezone). This will accomplish that goal.

Related

where does the t0000z come from?

{ "_id" : 1, "date" : ISODate("2019-06-23T00:00:00Z"), "tempsF" : [ 39.2, 53.6, 62.6 ] }
{ "_id" : 2, "date" : ISODate("2019-07-07T00:00:00Z"), "tempsF" : [ 57.2, 75.2, 51.8 ] }
{ "_id" : 3, "date" : ISODate("2019-10-30T00:00:00Z"), "tempsF" : [ 64.4, 42.8, 46.4 ] }
Where does the “T00:00:00Z” in “ISODate” come from?
to understand where did this come from in MongoDB database documents.
ISODate() is a helper function that’s built into MongoDB and it provides output in global standardised ISO 8601 date format that is “YYYY-MM-DDTHH:mm:ssZ”. Here “T” separates the date portion from the time-of-day portion. The Z on the end means UTC 11 (that is, an offset-from-UTC of zero hours-minutes-seconds). Also, that these default to 00:00:00 because time was not defined.

convert timestamp to ISO date in Mongodb version 2.4

I have mongodb version 2.4.10 installed. The structure of the document is :-
{
"_id" : ObjectId("5d15f245f4dda1e055091ae1"),
"name" : "test site service",
"starFromTimestamp" : NumberLong(1559275200),
"toTimestamp" : NumberLong(1561867200),
"uuid" : "ssg-5d15f245f2893825813309"
}
I excuted the following code in Mongo shell in order to convert timestamp to ISODate
db.servicesitegroup.find().forEach(function(doc) {
doc.startISODate=new Date(doc.starFromTimestamp);
db.servicesitegroup.save(doc);
})
The document got updated and the resultset looks like:-
{
"_id" : ObjectId("5d15f245f4dda1e055091ae1"),
"name" : "test site service",
"starFromTimestamp" : NumberLong(1559275200),
"toTimestamp" : NumberLong(1561867200),
"uuid" : "ssg-5d15f245f2893825813309",
"startISODate" : ISODate("1970-01-19T01:07:55.200Z")
}
If I use the timestamp converter, then the value of 1559275200 amounts to Friday, May 31, 2019 4:00:00 AM . Why is the timestamp not being converted to the correct value? Can anyone guide me here.
I needed to multiply the timestamp value with 1000 .
db.servicesitegroup.find().forEach(function(doc) {
doc.startISODate=new Date(doc.starFromTimestamp * 1000);
db.servicesitegroup.save(doc);
})
The code above gives me the correct output.

Mongo DB: Time based filtering

I have loaded data into mongoDB in below format:
{
"horizontalAccuracy" : 3.0,
"timestamp" : 1551390869,
"datetime" : ISODate("2019-02-28T18:30:00.000+0000"),
"month" : 3,
"year" : 2019,
"country" : "MYS"
}
Now I want to run a query on this records, select records which are created between 02:00PM to 04:00PM. I would be glad to have any suggestions.

Cannot search mongo db by timestamp

Hey I am having an issue with this line of code
db.eventrecords.find({ timestamp: { $gte:ISODate("2013-11-19T14:00:00Z"),
$lt: ISODate("2018-11-19T20:00:00Z") } })
This query and any similiar query using timestamp returns nothing even though I know there is data there.
Here is an example of that data returned with d.eventrecords.find()
{ "_id" : ObjectId("5a0c9e22da6e704174881b6c"), "userId" :
"59e0265c387d7d22a4f81533", "timestamp" : "Wed Nov 15 2017 15:05:54 GMT-
0500 (Eastern Standard Time)", "name" : "LOGIN" }
Can anyone shed light on why this query won't return data?
You can't do that because you store dates as string (not timestamp).

Emit a DateTime in the Map function of MongoDb

My map function looks like this:
map = function()
{
day = Date.UTC(this.TimeStamp.getFullYear(), this.TimeStamp.getMonth(), this.TimeStamp.getDate());
emit({day : day, store_id : this.Store_Id}, {count : 1});
}
TimeStamp is stored as date in the database, like this:
{ "TimeStamp" : "Mon Mar 01 2010 11:58:09 GMT+0000 (BST)", ...}
I need the "day" in the result collection to be stored as a date type as well, but it's stored as long (Epoch ticks) like this:
{ "_id" : { "day" : 1265414400000, "store_id" : 10}, "value" : { "count" : 7 } }
I tried changing the emit to something like this but didn't help:
emit({day : {"$date" : day},...)
Any ideas as to how to do that?
Date.utc is going to return miliseconds from epoch. So when you put your data back into the DB, you can use for example:
new Date(dateAsLong)
and it will be stored as the BSON date format.
earlier than mongo 1.7 it will show up in your hash as:
"Mon Mar 01 2010 11:58:09 GMT+0000 (BST)"
1.7+ it will appear as:
ISODate("2010-03-01T11:58:09Z")