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).
Related
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.
Type of document
{
"_id" : ObjectId("585232c2bbdfc4243ecf2670"),
"field1" : "value1",
"date" : "Mon Dec 19 2016 14:45:17 GMT+0530 (IST)",
"field2" : "value2",
"field3" : true
}
Query used:
db.myCollection.find({"date":{"$lt":new Date()}})
I want to run this query on 12:05 AM to fetch all past records upto
yesterday 23:59:59
It seems that the value is a string and not a Date.
fields of type Date should appear like this:
"date" : ISODate("2016-12-19T14:45:17.000Z");
and not like what you're seeing.
Make sure you save a Date object into the collection, and not a string representation of it.
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.
I′ve been using node-mongoskin to connect this two. Everything was ok until I queried some "date" field which I think should be returned as javascript′s Date object. But result′s type was string, which is odd (for me) and inconvenient.
Inserting looks something like this:
var doc = {
date: new Date(),
info: 'Some info'
}
db.users.insert( doc, {safe: true}, function(err, res) {
...
});
And result of above is (without _id field):
{ "date" : "Mon Oct 24 2011 18:00:57 GMT+0400 (MSK)", "info": "Some info" }
However, inserting with MongoDB Shell works just fine, except type of field is ISODate
> db.things.insert({ date: new Date() }); db.things.find();
{ "_id" : ObjectId("4eae9f2a34067b92db8deb40"), "date" : ISODate("2011-10-31T13:14:18.947Z") }
So, the question is: how should I insert documents to query date fields as Date object? What I want is setting fields on database-server-side. I just send something like null-fields, and db-server setting those for me using default mongo′s mechanisms.
Inserting timestamps (as native MongoDB timestamp) is also a problem, but it′s not such a big deal.
PS: No luck going through mongoskin and mongodb-native docs.
It was probably some bug in my code or the mongo driver. Now, the following works just fine:
db.collection.insert({d: new Date()});
Timestamps support described here: http://mongodb.github.com/node-mongodb-native/api-bson-generated/timestamp.html.
ISODate is the native way for mongo to store date. I use node-mongodb-native npm module and I store/retrieve javascript Date using new Date() idiom like in your examples. I don't know if it's a recent correction because I started node and Mongo in 2012, but using date was pretty straightforward for me.
JavaScript code:
collection.insert({"className" : "models.Action",
"title" : "Email",
"description" : "How are you today?",
"creationDate" : new Date("Fry, 4 May 2012 10:30:08 +0200 (CEST)"),
"creator" : dbref },
produced in mongoDB
db.action.find({"title":"Email"})
> db.action.find({"title":"Email"})
{ "className" : "models.Action", "title" : "Email", "description" : "How are you today?", "creationDate" : ISODate("2012-05-04T08:30:08Z"), "creator" : { "$ref" : "person", "$id" : ObjectId("4f995e4824ac8d68f63adf69") }, "_id" : ObjectId("4fa79e2e92c2a19a09000002") }
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")