Find 15 mins data with ObjectID field

2020-07-27 15:58发布

问题:

I am finding latest 15 minutes data with ObjectID.

Find Query:

db.data.find({ 
    _id: { 
        $gte: new ObjectId(
            Math.floor(new Date(ISODate().getTime() - 1000 * 60 * 15) / 1000).toString(16) + "0000000000000000"
        ) 
    } 
})

Output:

{ "_id" : ObjectId("57c3ef6837ff7057a2ad3cca"), "User" : "Karthick", age: 26 }

{ "_id" : ObjectId("57c3ef6837ff7057a2ad3cce"), "User" : "Raja", age: 29 }

But, I am trying to take the data dump,

mongodump --query '{ _id: { $gte: new ObjectId(Math.floor(new Date(ISODate().getTime() - 1000 * 60 * 15) / 1000).toString(16) + "0000000000000000") } }' --db test --collection data --username abcdef --password abc@123 --authenticationDatabase admin -o "mongodump"

Error

Failed: error parsing query as json: invalid character 't' in literal MaxKey (expecting 'x')   

I am getting the above error. How do I solve this issue or how do I put 15 minutes data dump in cronjob?

回答1:

The problem is your query is not valid JSON as it contains JS expressions to be evaluated (your calculations with the date).
So you basically have to make a script or do some shell acrobatics which generates the query JSON prior passing it to mongoexport

You may find this link useful which does exactly that with python

In addition I did (quick & dirty) something similar with node, i.e. created a file query.js with this content which essentially creates your query JSON and writes it to the console

var oid = Math.floor(new Date(new Date().getTime() - 1000 * 60 * 15) / 1000).toString(16) + "0000000000000000";
console.log('{ "_id": { "$gte": new ObjectId("' + oid + '") } }');

so that you now can use it in your shell like so

mongoexport ... --query "$(node query.js)" ...

Hope that helps



标签: mongodb