Thanks.
2. db.posts.find({"created_on": {"$gte": start, "$lt": end}})
Where start and end are dates.
--
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.
There is more information about the Mongo ObjectID Datatype here:
http://www.mongodb.org/display/DOCS/Object+IDs
Here is how it is done in the Mongo JS Shell:
> my_id = db.test_collection.findOne()._id
ObjectId("4ed677329217cbcb7801c448")
> my_id.getTimestamp()
ISODate("2011-11-30T18:34:26Z")
Just about all of the language drivers implement methods for
extracting these timestamps. Please refer to the relevant api docs
for details.
Querying based on the timestamps embedded in ObjectIds is possible
with a little bit of legwork. I have been informed that this is "not
recommended", and if possible, a new field containing the creation
date should be added. However, here is how it is done:
1) Find the number of seconds from the Epoch (seconds, NOT
milliseconds) of your desired query date.
2) Convert this number to a hexidecimal string
3) The hexadecimal string should be 8 characters long. Add 16 zeros
to it, to bring the length to 24 characters
4) Create a new ObjectId based on this string
5) Query against this new ObjectId.
Here is an example done in the JS shell:
> db.time.drop() // ensure that the "time" collection is empty for this example
true
> db.time.save({x:1})
> db.time.save({x:2})
> db.time.save({x:3})
> query_time = new Date() //create a new date object in the middle of the inserted objects
ISODate("2011-12-01T00:27:26.316Z")
> db.time.save({x:4})
> db.time.save({x:5})
> db.time.save({x:6})
> ms = query_time.getTime()
1322699246316
> sec = ms/1000
1322699246.316
> roundsec = Math.floor(sec)
1322699246
> hex = roundsec.toString(16)
4ed6c9ee
> id_string = hex + "0000000000000000"
4ed6c9ee0000000000000000
> my_id = ObjectId(id_string)
ObjectId("4ed6c9ee0000000000000000")
> db.time.find({_id:{$lt:my_id}}) //find the documents created before query_time
{ "_id" : ObjectId("4ed6c9ce2edc10a978c6ce9e"), "x" : 1 }
{ "_id" : ObjectId("4ed6c9d42edc10a978c6ce9f"), "x" : 2 }
{ "_id" : ObjectId("4ed6c9d92edc10a978c6cea0"), "x" : 3 }
> db.time.find({_id:{$gt:my_id}}) //find the documents created after query_time
{ "_id" : ObjectId("4ed6c9f32edc10a978c6cea1"), "x" : 4 }
{ "_id" : ObjectId("4ed6c9f62edc10a978c6cea2"), "x" : 5 }
{ "_id" : ObjectId("4ed6c9f92edc10a978c6cea3"), "x" : 6 }
>
Hopefully, the above will help you to accomplish what you need to do.
Good luck!
On Nov 30, 7:45 pm, Marc <m...@10gen.com> wrote:
> Extracting the time stamp from Mongo ObjectIds is explained in the
> Mongo Document "Optimizing Object IDs"http://www.mongodb.org/display/DOCS/Optimizing+Object+IDs#OptimizingO....