0

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

user1665355
  • 3,324
  • 8
  • 44
  • 84
  • I am also facing same issue, How do i solve this problem. – Karthickkumar Nagaraj Aug 29 '16 at 12:24
  • Hi, try this link: https://sheharyar.me/blog/regular-mongo-backups-using-cron/, dumps the whole db with cron. – user1665355 Aug 29 '16 at 13:08
  • how to resolve the below issue. mongodump --db test --collection data --query '{"created_at":{"$gte":new Date(ISODate().getTime()-1000*60*15)}}' --username abcd --password abc@123 --authenticationDatabase admin -o "mongodump_15mins_data" ERROR: 2016-08-29T12:17:46.802+0000 Failed: error parsing query as json: invalid character '.' after constructor argument when i run mongodump query above error i got it. – Karthickkumar Nagaraj Aug 29 '16 at 13:53
  • Did not find solution for that – user1665355 Aug 29 '16 at 13:54

1 Answers1

1

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

Community
  • 1
  • 1