问题
I want to extract the timestamp from my ObjectID with a json query, as I would like to use mongodump but only dump data between certain dates. I dont wanna put my timestamps somewhere else than the ObjectID as I need the database to be as small as possible.
Is there a way to to exstract the timestamp from ObjectID with a simple json query that mongodump accepts?
回答1:
You can do this fairly simply, at doc page Mongo Extended JSON (which is quite well hidden) you can find a table describing how to represent mongo extended datatypes in JSON. As you probably know, the first 4 bytes of ObjectId
represent the timestamp, this maps directly to 8 first characters in the hex string. Thus, the following should work:
jhonkola@ubuntu:~$ mongoexport -d so_test -c example -q '{"_id" : {"$gt" : {"$oid" : "4fad36290000000000000000"}}}'
connected to: 127.0.0.1
{ "_id" : { "$oid" : "4fad3629a8bbba98829d5c1e" }, "a" : "bar", "b" : 2 }
{ "_id" : { "$oid" : "4fad362ea8bbba98829d5c1f" }, "a" : "baz", "b" : 3 }
{ "_id" : { "$oid" : "4fad3635a8bbba98829d5c20" }, "a" : "buzz", "b" : 4 }
{ "_id" : { "$oid" : "4fad363ca8bbba98829d5c21" }, "a" : "fizz", "b" : 5 }
exported 4 records
jhonkola@ubuntu:~$
Below are all the commands used for the example for reference.
> use so_test
switched to db so_test
> db.example.insert({a: "foo", b: 1})
> db.example.insert({a: "bar", b: 2})
> db.example.insert({a: "baz", b: 3})
> db.example.insert({a: "buzz", b: 4})
> db.example.insert({a: "fizz", b: 5})
> db.example.find()
{ "_id" : ObjectId("4fad3620a8bbba98829d5c1d"), "a" : "foo", "b" : 1 }
{ "_id" : ObjectId("4fad3629a8bbba98829d5c1e"), "a" : "bar", "b" : 2 }
{ "_id" : ObjectId("4fad362ea8bbba98829d5c1f"), "a" : "baz", "b" : 3 }
{ "_id" : ObjectId("4fad3635a8bbba98829d5c20"), "a" : "buzz", "b" : 4 }
{ "_id" : ObjectId("4fad363ca8bbba98829d5c21"), "a" : "fizz", "b" : 5 }
> db.example.find({_id : {$gt : ObjectId("4fad362e0000000000000000")}})
{ "_id" : ObjectId("4fad362ea8bbba98829d5c1f"), "a" : "baz", "b" : 3 }
{ "_id" : ObjectId("4fad3635a8bbba98829d5c20"), "a" : "buzz", "b" : 4 }
{ "_id" : ObjectId("4fad363ca8bbba98829d5c21"), "a" : "fizz", "b" : 5 }
>
bye
jhonkola@ubuntu:~$ mongodump -d so_test -c example -q '{"_id" : {"$gt" : {"$oid" : "4fad36290000000000000000"}}}'
connected to: 127.0.0.1
DATABASE: so_test to dump/so_test
so_test.example to dump/so_test/example.bson
4 objects
jhonkola@ubuntu:~$ mongoexport -d so_test -c example -q '{"_id" : {"$gt" : {"$oid" : "4fad36290000000000000000"}}}'
connected to: 127.0.0.1
{ "_id" : { "$oid" : "4fad3629a8bbba98829d5c1e" }, "a" : "bar", "b" : 2 }
{ "_id" : { "$oid" : "4fad362ea8bbba98829d5c1f" }, "a" : "baz", "b" : 3 }
{ "_id" : { "$oid" : "4fad3635a8bbba98829d5c20" }, "a" : "buzz", "b" : 4 }
{ "_id" : { "$oid" : "4fad363ca8bbba98829d5c21" }, "a" : "fizz", "b" : 5 }
exported 4 records
来源:https://stackoverflow.com/questions/10552621/mongodb-extract-timestamp-from-objectid-in-json-query