[SERVER-857] add get_timestamp method to ObjectId representation in the server's embedded interpreter Created: 31/Mar/10  Updated: 12/Jul/16  Resolved: 15/Jun/10

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: 1.5.3

Type: Improvement Priority: Major - P3
Reporter: Andrew Burrows Assignee: Aaron Staple
Resolution: Done Votes: 4
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

See emails below for an example:

I don't think the ObjectId representation in the server's embedded
interpreter has a method to extract the timestamp currently. Could
open a separate JIRA for that as well.

  • Hide quoted text -

On Wed, Mar 31, 2010 at 3:19 PM, Andrew Burrows <burrowsa@gmail.com> wrote:
> Yes thank you. That makes perfect sense now. What about grouping by time
> stamp is that possible?
>
> On 31 March 2010 20:04, Michael Dirolf <mike@10gen.com> wrote:
>>
>> You would do it the way you did in your example:
>>
>> {"_id": {"$gt": dummy_id}}
>>
>> Where dummy_id has been manually created from a timestamp or created
>> using a helper if we add one. Make more sense?
>>
>> On Wed, Mar 31, 2010 at 2:58 PM, Andrew Burrows <burrowsa@gmail.com>
>> wrote:
>> > sorry, I don't understand you. If I could create a dummy_id (or you add
>> > the
>> > function you mentioned) how would I do the query?
>> >
>> > On 31 March 2010 19:13, Michael Dirolf <mike@10gen.com> wrote:
>> >>
>> >> Right now you'd need to manually create a dummy _id value w/ the right
>> >> timestamp and use that to sort on. We could probably add an
>> >> ObjectId.from_datetime() method to make that a bit nicer. Mind filing
>> >> a JIRA for this?
>> >>
>> >> On Wed, Mar 31, 2010 at 2:04 PM, Andrew Burrows <burrowsa@gmail.com>
>> >> wrote:
>> >> > Hi
>> >> >
>> >> > I'm currently developing an application on top of mongo. At the
>> >> > moment
>> >> > I have a time stamp field in my documents which allows me to query
>> >> > documents created in the last 24 hours and do a group by where I
>> >> > group
>> >> > into 5 minute buckets. I've just been reading that the _id field
>> >> > contains a time stamp and thought I could just use this and drop my
>> >> > extra field. Is this possible? How to I refer to the time stamp part
>> >> > of _id in my query?
>> >> >
>> >> > Here is some example python of the kind of thing I'm doing:
>> >> >
>> >> > # just a function to get me the time as seconds since epoc
>> >> > def ts(dt):
>> >> > return int(mktime(dt.timetuple()))
>> >> >
>> >> > # here is an insert
>> >> > db.box.insert(dict(ts=ts(datetime.now()), x=randint(0, 500),
>> >> > y=randint(0, 100)))
>> >> >
>> >> > # here is a query
>> >> > db.box.group(key = 'function(obj){ return

{ ts : 300 * >> >> > parseInt(obj.ts/300) }

; }',
>> >> > condition = dict(ts=

{'$gt': ts(datetime.now() - >> >> > timedelta(hours=24)) }

),
>> >> > initial = dict( xmax=0, ysum=0, count=0 ),
>> >> > reduce = 'function(obj,prev)

{ prev.xmax = >> >> > Math.max(prev.xmax, obj.x); prev.ysum += obj.y; prev.count++; }',
>> >> > finalize = 'function(out){ out.yavg = >> >> > out.ysum / out.count }')
>> >> >
>> >> > Ideally I'd replace it with something like:
>> >> >
>> >> > # here is an insert - look no ts
>> >> > db.box.insert(dict(x=randint(0, 500), y=randint(0, 100)))
>> >> >
>> >> > # here is a query
>> >> > db.box.group(key = 'function(obj){ return { ts : 300 * >> >> > parseInt(obj._id.timestamp/300) }; }',
>> >> > condition = {'_id.timestamp' : {'$gt':
>> >> > ts(datetime.now() - timedelta(hours=24)) }},
>> >> > initial = dict( xmax=0, ysum=0, count=0 ),
>> >> > reduce = 'function(obj,prev) { prev.xmax =>> >> > Math.max(prev.xmax, obj.x); prev.ysum += obj.y; prev.count++; }

',
>> >> > finalize = 'function(out)

{ out.yavg = >> >> > out.ysum / out.count }

')
>> >> >
>> >> > Is this kind of thing possible? what property/method do I need to use
>> >> > on _id?
>> >> >
>> >> > Thanks
>> >> > Andy
>> >> >



 Comments   
Comment by auto [ 16/Jun/10 ]

Author:

{'login': 'astaple', 'name': 'Aaron', 'email': 'aaron@10gen.com'}

Message: SERVER-857 test
http://github.com/mongodb/mongo/commit/bbc51275c0ecf6e9bbfb11a167355a572a42b9c3

Comment by auto [ 15/Jun/10 ]

Author:

{'login': 'erh', 'name': 'Eliot Horowitz', 'email': 'eliot@10gen.com'}

Message: more tests for SERVER-857
http://github.com/mongodb/mongo/commit/c5d975b2b52fb093922c2ee5f4661f82b3ffa2cc

Comment by auto [ 15/Jun/10 ]

Author:

{'login': '', 'name': 'Van Nguyen', 'email': 'van@metamoki.com'}

Message: SERVER-857 shell ObjectId.getTimestamp() support
http://github.com/mongodb/mongo/commit/0b780fdcda3f6a7fd1093e610f43b9356e8f680a

Comment by Van Nguyen (vnguyen) [ 14/Jun/10 ]

From within a js shell map function, it is easy to get the date:

var d = new Date(parseInt(this._id.toString().slice(0,8), 16)*1000),
date_str = [d.getMonth(), d.getDate(), d.getFullYear()].join("/");

date_str is also easy to customize so you can use it however you need it. If you want this functionality outside of a mapReduce setting, just replace "this._id" with code to access a document's _id field.

Comment by Justin Dearing [ 14/Jun/10 ]

I was looking for a way to do this from the mongo shell this morning. It would be really useful.

It would be similarly beneficial to be able to get timestamp and mac address for a UUID from the shell, but thats a separate ticket..

Comment by Mathias Stearn [ 14/Apr/10 ]

Aaron, could you add this to the JS API?

Comment by auto [ 14/Apr/10 ]

Author:

{'login': 'RedBeard0531', 'name': 'Mathias Stearn', 'email': 'mathias@10gen.com'}

Message: ObjectID from Date_t SERVER-857
http://github.com/mongodb/mongo/commit/7e25fdb8e8fd9e080e62a6a86c1804699f3119f7

Comment by auto [ 14/Apr/10 ]

Author:

{'login': 'RedBeard0531', 'name': 'Mathias Stearn', 'email': 'mathias@10gen.com'}

Message: Convert OID to datetimes SERVER-857
http://github.com/mongodb/mongo/commit/0cae84589ef3c20a8c2ffbf09a5e1fe27ebbb3e9

Generated at Thu Feb 08 02:55:22 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.