filter on date objects which are Strings

129 views
Skip to first unread message

Matti van Aert

unread,
Mar 26, 2014, 10:01:53 AM3/26/14
to mongod...@googlegroups.com
Hi,

I have a mongo database and save log data in the DB. I will convert the XML to JSON and the date objects in XML are not date objects in JSON/Mongo.

Below an example of the collection:

                {

"@DateTime" : "2011-12-29 16:11:20.443590+0000",

"MessageType" : "SYSTEM_IN_USE"

},

{

"@DateTime" : "2011-12-29 16:26:20.445550+0000",

"MessageType" : "SYSTEM_IN_USE"

},

{

"@DateTime" : "2011-12-29 16:31:38.615997+0000",

"MessageType" : "STUDY_ENDED"

},

{

"@DateTime" : "2011-12-29 16:41:20.445398+0000",

"MessageType" : "SYSTEM_IN_USE"

},

{

"@DateTime" : "2011-12-29 16:56:20.441512+0000",

"MessageType" : "SYSTEM_IDLE"

},

{

"@DateTime" : "2011-12-29 17:11:20.445398+0000",

"MessageType" : "SYSTEM_IDLE"

},

{

"@DateTime" : "2011-12-29 17:26:20.442402+0000",

"MessageType" : "SYSTEM_IDLE"

},

{

"@DateTime" : "2011-12-29 17:41:20.442396+0000",

"MessageType" : "SYSTEM_IDLE"

}



Is it possible to create a query with a filter on these @DateTime objects using a real Date object?


Regards,

Matti

Asya Kamsky

unread,
Mar 28, 2014, 9:55:19 AM3/28/14
to mongodb-user
It's not possible - you should probably write a custom data importer
that converts dates into proper types and at the same time consider
changing the schema from what you have. Given the types of queries
you want to ask (about times system is idle vs. in use) this schema
isn't particularly good for those types of queries.

Asya
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user"
> group.
>
> For other MongoDB technical support options, see:
> http://www.mongodb.org/about/support/.
> ---
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mongodb-user...@googlegroups.com.
> To post to this group, send email to mongod...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mongodb-user.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mongodb-user/c9ec42a4-be87-4b32-a40b-55ee97e8a1c2%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Glenn Maynard

unread,
Mar 28, 2014, 10:59:46 AM3/28/14
to mongod...@googlegroups.com
You can convert the Date object of your language to the same ISO-8601 format, and use that to make queries.  For example:

db.coll.find({
    '@DateTime': {
        '$gte': '2011-12-28 14:30:00',
        '$lt': '2012-01-01 00:00:00'
    }
})

This requires that the timezone is always the same, ISO-8601 is in local time.  (Alternatively, if the timezone differs, then that lets you make queries against local time.  That's also useful, but not always what's wanted.)  It also requires that you consistently use the same ISO-8601 variant.  For example, if the date/time separator is sometimes a T instead of a space this won't work.  But, as long as you're consistent, this works fine.




For more options, visit https://groups.google.com/d/optout.



--
Glenn Maynard

Reply all
Reply to author
Forward
0 new messages