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")
Related
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).
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 have a document that looks like this:
{
"_id" : ObjectId("570fc2381d4899be8a8ec9d9"),
"statuses" : [
{
"created_at" : "Wed Apr 13 09:56:39 +0000 2016",
"id" : 7.20188946337153e+017,
"id_str" : "720188946337153024",
"text" : "RT #BCC_Assicura: #FormulaAuto la #polizza #Auto e #Moto economica BccPordenonese - #BCC #Assicurazioni #Click2go"
},
{
"created_at" : "Wed Apr 13 09:40:13 +0000 2016",
"id" : 7.20184809658708e+017,
"id_str" : "720184809658707970",
"text" : "Auto e moto storiche, vademecum su assicurazione e bollo - \n#autostoriche #bollo #RCauto #ASI #FMI"
}
]}
How do I query for all the records where the variable text contains the string "assicur"?
Thank you!
One possibility would be to use a regex;
> db.test.find({"statuses.text":{$regex: 'assicur'}})
That said, this will not be possible to index in mongodb, so it's probably best done along with other filters that cut the documents down to a small set before doing the string matching.
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'm stumped on casbah find. I'm trying to pull back all documents from a MongoDB between date1 and date2. Here's an example set of mongo docs:
{ "_id" : NumberLong("1285248838000"), "openTime" : "Thu Sep 23 2010 06:33:58 GMT-0700 (PDT)", "closeTime" : "Thu Sep 23 2010 06:36:15 GMT-0700 (PDT)", "timeInTrade" : "00:02:17", "direction" : "Long", "size" : 1, "outcome" : "Loss" }
{ "_id" : NumberLong("1285595711000"), "openTime" : "Mon Sep 27 2010 06:55:11 GMT-0700 (PDT)", "closeTime" : "Mon Sep 27 2010 06:57:37 GMT-0700 (PDT)", "timeInTrade" : "00:02:26", "direction" : "Short", "size" : 1, "outcome" : "Win"}
{ "_id" : NumberLong("1285594773000"), "openTime" : "Mon Sep 27 2010 06:39:33 GMT-0700 (PDT)", "closeTime" : "Mon Sep 27 2010 06:41:47 GMT-0700 (PDT)", "timeInTrade" : "00:02:14", "direction" : "Short", "size" : 1, "outcome" : "Win" }
{ "_id" : NumberLong("1286289026000"), "openTime" : "Tue Oct 05 2010 07:30:26 GMT-0700 (PDT)", "closeTime" : "Tue Oct 05 2010 07:36:23 GMT-0700 (PDT)", "timeInTrade" : "00:05:57", "direction" : "Short", "size" : 2, "outcome" : "Loss"}
So, let's say I want to pull back the documents from Sep 27. How would I go about doing that?
In the casbah documentation, it looks like I could construct a builder like this:
val dt = new DateTime("2010-09-27T00:00:00.000-08:00")
val bldr = MongoDBObject.newBuilder
bldr += "openTime" $gte dt $lt dt.plusDays(1)
val result = coll.find(bldr.result)
In my IDE (Netbeans), this will not compile because "$gte is not a member of java.lang.String". I had similar results with the other documented ways to construct my filter.
I suspect that the next problem I would have is that it doesn't know how to compare the dates because they are stored as joda DateTimes, so if anyone has experience with these problems, I would greatly appreciate some guidance.
Thanks,
John
FOLLOW-UP:
I've got a partial solution, but only because I was using the milliseconds as the _id. Here is some code that works for that case:
val begin = dt.getMillis
val end = dt.plusDays(1).getMillis
val json = "{ '_id' : { '$gte' : " + begin + " , '$lt' : " + end + "}}"
val dbObject = JSON.parse(json).asInstanceOf[DBObject];
for (x <- coll.find(dbObject)) println(x)
I'm still interested in learning about a solution that works on DateTime instead of the Long millis...
opentime is stored as a string on the Mongo side. Your $gte function won't work b/c string comparison won't work.
To make this work, you'll have to use a $where clause and a function that performs the comparison correctly. So you'll basically have to write a javascript function that correctly interprets the JODA time. You'll then have to include that function with your DB call or you'll have to store it server-side and proceed from there.
Here are some details on the where clause. Here are some details on server-side code execution.