user1665355
user1665355

Reputation: 3393

ISODate in --queryFile

I use the below mongodump code to dump records based on a date, in an .sh file:

$MONGODUMP_PATH -h $MONGO_HOST:$MONGO_PORT 
-d $MONGO_DATABASE  -c $MONGO_COLLECTION 
--queryFile subset.json

subset.json:

{ "TheDate": { "$gte": new Date(new Date().setDate(new Date().getDate() - 1)) } }

That does not work, and produces an error:

Failed: error parsing query as json: invalid character '.' after constructor argument

But if I change subset.json to include a static date value, it works:

{ "TheDate": { "$gte": ISODate("2016-06-14T07:12:23.051Z") } }

Where ISODate("2016-06-14T07:12:23.051Z") equals new Date(new Date().setDate(new Date().getDate() - 1)) as previously.

But I would need a dynamic value for date, as in the first case. Have been looking for a solution online but cant find any...

Any ideas? Best Regards

Upvotes: 0

Views: 1169

Answers (1)

Karthickkumar Nagaraj
Karthickkumar Nagaraj

Reputation: 577

Hope it working for me !!!

The problem is your query is not valid JSON as it contains JS expressions to be evaluated (your calculations with the date).

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 * 60 * 24) / 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)" ...

Please refer below link,

Find 15 mins data with ObjectID field

Upvotes: 1

Related Questions