mongodb: extract timestamp from ObjectID in json query

荒凉一梦 提交于 2019-11-30 22:16:22

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!