mongodb:从 json 查询中的 ObjectID 中提取时间戳 [英] mongodb: extract timestamp from ObjectID in json query

查看:15
本文介绍了mongodb:从 json 查询中的 ObjectID 中提取时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 json 查询从我的 ObjectID 中提取时间戳,因为我想使用 mongodump 但只在特定日期之间转储数据.我不想把我的时间戳放在 ObjectID 以外的地方,因为我需要数据库尽可能小.

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.

有没有办法通过 mongodump 接受的简单 json 查询从 ObjectID 中提取时间戳?

推荐答案

你可以很简单地做到这一点,在文档页面 Mongo Extended JSON (隐藏得很好)你可以找到一个描述如何在 JSON 中表示 mongo 扩展数据类型的表格.您可能知道,ObjectId 的前 4 个字节表示时间戳,这直接映射到十六进制字符串中的前 8 个字符.因此,以下应该有效:

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

这篇关于mongodb:从 json 查询中的 ObjectID 中提取时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆