Extracting month from _id timestamp

363 views
Skip to first unread message

Voltron

unread,
Jan 1, 2011, 12:06:54 PM1/1/11
to mongodb-user
Happy new year to all!

I am having problems with a query. I would like to select all the
documents of a certain month usind the _id field. I am using Pymongo

I know that I can get a datetime object using the
"doc['_id'].generation_time" property, and the month in the datetime
object returned. How can construct a query to do this all at once?

Thanks

Eli Jones

unread,
Jan 1, 2011, 12:11:15 PM1/1/11
to mongod...@googlegroups.com
Maybe the documentation will have some clues:




--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Andreas Jung

unread,
Jan 1, 2011, 12:11:08 PM1/1/11
to mongod...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

If you need to query my creation month then create a dedicated
'created_month' property that you fill upon creation time.

- -aj
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQGUBAEBAgAGBQJNH2ArAAoJEADcfz7u4AZjAdsLvj26PMQY61bXMjgj6mzbY/Rx
PThQ+vr4Uhb6y2QM6TRgm66Gi2YfEWfhO+ASKVER2GPa0zVvqVmcaEfqNE5GyAVe
CEQC1BW0ZrkyzLc7VM4AUcVdy4jM2elTlaJfZS5pElcm0QBKljcaXjLuSFGbqyol
pBmYXI+r/ux0BcUeZ2D6fPjau/rY/0WKOTjUTmS/r7v+GKbTq4ec60QvVgnVxZym
YxsJ3h8M52ZV3ldg2EefyyerTrq+MdaxC3eSFtrC7SUeiroJWAIB7nBfHol63qGh
PalwWpEUKmGLHdqkddI6OIe+21XImECwk1M4nBW2PAQCFZLvHuREKDthGlTIalVU
HwZRqNtbuw3IS1Gu0UK3qH5zPzHqpCZ60JCjP63YwBudxToHSnpAT1ZhJqKqPNI8
lc/xAkGP+dvMMQt6AheZPLStQ3Ysh/LaCfJctLP1GG6I5N2P313zk/+GvXI5GdOC
Kl7yhYfsKDN5eMWqWQ+qsSTiOZYTEY8=
=RZEg
-----END PGP SIGNATURE-----

lists.vcf

Voltron

unread,
Jan 1, 2011, 12:26:18 PM1/1/11
to mongodb-user
Thats what I was doing Andreas, I thought it was redundant since the
timestamp is stored in the ID. So its not worth the extra code, I
would just add month and "day_created" then. Thanks



On Jan 1, 6:11 pm, Andreas Jung <li...@zopyx.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Voltron wrote:
> > Happy new year to all!
>
> > I am having problems with a query. I would like to select all the
> > documents of a certain month usind the _id field. I am using Pymongo
>
> > I know that I can get a datetime object using the
> > "doc['_id'].generation_time" property, and the month in the datetime
> > object returned. How can construct a query to do this all at once?
>
> If you need to query my creation month then create a dedicated
> 'created_month' property that you fill upon creation time.
>
> - -aj
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.11 (Darwin)
> Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org/
>
> iQGUBAEBAgAGBQJNH2ArAAoJEADcfz7u4AZjAdsLvj26PMQY61bXMjgj6mzbY/Rx
> PThQ+vr4Uhb6y2QM6TRgm66Gi2YfEWfhO+ASKVER2GPa0zVvqVmcaEfqNE5GyAVe
> CEQC1BW0ZrkyzLc7VM4AUcVdy4jM2elTlaJfZS5pElcm0QBKljcaXjLuSFGbqyol
> pBmYXI+r/ux0BcUeZ2D6fPjau/rY/0WKOTjUTmS/r7v+GKbTq4ec60QvVgnVxZym
> YxsJ3h8M52ZV3ldg2EefyyerTrq+MdaxC3eSFtrC7SUeiroJWAIB7nBfHol63qGh
> PalwWpEUKmGLHdqkddI6OIe+21XImECwk1M4nBW2PAQCFZLvHuREKDthGlTIalVU
> HwZRqNtbuw3IS1Gu0UK3qH5zPzHqpCZ60JCjP63YwBudxToHSnpAT1ZhJqKqPNI8
> lc/xAkGP+dvMMQt6AheZPLStQ3Ysh/LaCfJctLP1GG6I5N2P313zk/+GvXI5GdOC
> Kl7yhYfsKDN5eMWqWQ+qsSTiOZYTEY8=
> =RZEg
> -----END PGP SIGNATURE-----
>
>  lists.vcf
> < 1KViewDownload

Sam Millman

unread,
Jan 1, 2011, 6:23:56 PM1/1/11
to mongod...@googlegroups.com
The problem is that the timestamp is used as part of the calculation in the ID it is not actually in there, plus the ID is a hash (md5 I believe) so it is one way. Just add a ts time stamp using the drivers MongoDate object that way it is not redundant and can be used for more than just month.

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.




--
Bow Chicka Bow Wow

Sam Millman

unread,
Jan 1, 2011, 6:26:51 PM1/1/11
to mongod...@googlegroups.com
Sorry for double post but:


"I know that I can get a datetime object using the
"doc['_id'].generation_time" property, and the month in the datetime
object returned. How can construct a query to do this all at once?"

Using the drivers mongo date object will allow you to do this, ie in php:

getCollection("users")->find(array("ts"=>new MognoDate(date(time()))))

Will find everything made today. I am sure you can do similar in pymongo

Scott Hernandez

unread,
Jan 1, 2011, 6:34:58 PM1/1/11
to mongod...@googlegroups.com
You can easily get docs for a given month, for a given year, but not
the same month across (multiple) years with just an ObjectId.

On Sat, Jan 1, 2011 at 3:23 PM, Sam Millman <smil...@nhbs.co.uk> wrote:
> The problem is that the timestamp is used as part of the calculation in the
> ID it is not actually in there, plus the ID is a hash (md5 I believe) so it
> is one way. Just add a ts time stamp using the drivers MongoDate object that
> way it is not redundant and can be used for more than just month.

It is not hashed, and the date (in seconds) are the first 4 bytes.
http://mongodb.org/display/DOCS/Object+IDs

Andrew Armstrong

unread,
Jan 1, 2011, 7:54:22 PM1/1/11
to mongodb-user
You are better off just recording this information explicitly.

On Jan 2, 10:34 am, Scott Hernandez <scotthernan...@gmail.com> wrote:
> You can easily get docs for a given month, for a given year, but not
> the same month across (multiple) years with just an ObjectId.
>
> On Sat, Jan 1, 2011 at 3:23 PM, Sam Millman <smill...@nhbs.co.uk> wrote:
> > The problem is that the timestamp is used as part of the calculation in the
> > ID it is not actually in there, plus the ID is a hash (md5 I believe) so it
> > is one way. Just add a ts time stamp using the drivers MongoDate object that
> > way it is not redundant and can be used for more than just month.
>
> It is not hashed, and the date (in seconds) are the first 4 bytes.http://mongodb.org/display/DOCS/Object+IDs

Voltron

unread,
Jan 2, 2011, 7:35:56 AM1/2/11
to mongodb-user
Ok, just for future use, is there someone that can write this query in
Pymongo? I do not need the docs to the API, I know how to use the
datatime and objectid field to get the present time and othere
properties and I have been using a separate timestamp field in the
past without problems. I just wanted to reduce the data redundancy

I could, in anothere circumstance need to write something equally
complicated, I found nothing similarly advanced in the docs

Thanks
Reply all
Reply to author
Forward
0 new messages