Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-857

add get_timestamp method to ObjectId representation in the server's embedded interpreter

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.5.3
    • Affects Version/s: None
    • Component/s: Querying
    • Labels:
      None

      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
      >> >> >

            Assignee:
            aaron Aaron Staple
            Reporter:
            burrowsa@gmail.com Andrew Burrows
            Votes:
            4 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: