3
提取物戳我想用一個JSON查詢提取從我的對象ID時間戳,因爲我想用mongodump但只有某些轉儲日期之間的數據。我不想把我的時間戳放在ObjectID以外的地方,因爲我需要數據庫儘可能小。MongoDB的:從對象ID在JSON查詢
是否有向exstract從對象ID時間戳與mongodump接受一個簡單的JSON查詢的方法嗎?
提取物戳我想用一個JSON查詢提取從我的對象ID時間戳,因爲我想用mongodump但只有某些轉儲日期之間的數據。我不想把我的時間戳放在ObjectID以外的地方,因爲我需要數據庫儘可能小。MongoDB的:從對象ID在JSON查詢
是否有向exstract從對象ID時間戳與mongodump接受一個簡單的JSON查詢的方法嗎?
你可以很簡單地做到這一點,在文檔頁Mongo Extended JSON(這是很好的隱藏),你可以找到一張表,描述如何用JSON表示mongo擴展數據類型。您可能知道,ObjectId
的前4個字節表示時間戳,它直接映射到十六進制字符串中的8個第一個字符。因此,以下應該工作:
[email protected]:~$ 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
[email protected]:~$
以下是用於示例供參考的所有命令。
> 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
[email protected]:~$ 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
[email protected]:~$ 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