datetime - Find objects between two dates MongoDB -
i've been playing around storing tweets inside mongodb, each object looks this:
{ "_id" : objectid("4c02c58de500fe1be1000005"), "contributors" : null, "text" : "hello world", "user" : { "following" : null, "followers_count" : 5, "utc_offset" : null, "location" : "", "profile_text_color" : "000000", "friends_count" : 11, "profile_link_color" : "0000ff", "verified" : false, "protected" : false, "url" : null, "contributors_enabled" : false, "created_at" : "sun may 30 18:47:06 +0000 2010", "geo_enabled" : false, "profile_sidebar_border_color" : "87bc44", "statuses_count" : 13, "favourites_count" : 0, "description" : "", "notifications" : null, "profile_background_tile" : false, "lang" : "en", "id" : 149978111, "time_zone" : null, "profile_sidebar_fill_color" : "e0ff92" }, "geo" : null, "coordinates" : null, "in_reply_to_user_id" : 149183152, "place" : null, "created_at" : "sun may 30 20:07:35 +0000 2010", "source" : "web", "in_reply_to_status_id" : { "floatapprox" : 15061797850 }, "truncated" : false, "favorited" : false, "id" : { "floatapprox" : 15061838001 }
how write query checks created_at , finds objects between 18:47 , 19:00? need update documents dates stored in specific format?
querying date range (specific month or day) in mongodb cookbook has explanation on matter, below tried out myself , seems work.
items.save({ name: "example", created_at: isodate("2010-04-30t00:00:00.000z") }) items.find({ created_at: { $gte: isodate("2010-04-29t00:00:00.000z"), $lt: isodate("2010-05-01t00:00:00.000z") } }) => { "_id" : objectid("4c0791e2b9ec877893f3363b"), "name" : "example", "created_at" : "sun may 30 2010 00:00:00 gmt+0300 (eest)" }
based on experiments need serialize dates format mongodb supports, because following gave undesired search results.
items.save({ name: "example", created_at: "sun may 30 18.49:00 +0000 2010" }) items.find({ created_at: { $gte:"mon may 30 18:47:00 +0000 2015", $lt: "sun may 30 20:40:36 +0000 2010" } }) => { "_id" : objectid("4c079123b9ec877893f33638"), "name" : "example", "created_at" : "sun may 30 18.49:00 +0000 2010" }
in second example no results expected, there still 1 gotten. because basic string comparison done.
Comments
Post a Comment