Mongodb export data updated in last 7 days with mongoexport

I am trying to export data updated in mongodb in last 7 days with mongoexport command from linux terminal I am using the following command

mongoexport --db your_database --collection your_collection --query ‘{ “updatedAt”: { “$gt”: new Date(new Date().getTime() - (7 * 24 * 60 * 60 * 1000)) } }’ --out output.json

but it’s throwing error not is not a validjson invalid character ‘.’ after constructor argument

I found this which looks like a similar issue, basically use $expr instead of the date constructor in the query filter:

I have edited the time and run the command with expr from rediff bt still throwing error too many postional arguments

I got that when playing as well, I swapped to using /query:“xxxxx”

What’s your exact command you’re running now?

@John_Sewell the command i am running is
mongoexport --db your_database --collection your_collection --fields field1 --query ‘{ “$expr”: { “$gt”: [ “$createdAt”, { “$dateSubtract”: { “startDate”: new Date(), “unit”: “second”, “amount”: 604800 } } ] } }’ --out output.csv

I am trying to get last 7 days data

If you look at the example I linked to, they use the $$NOW variable instead, I tested this locally and it seemed to work:

mongoexport --db Cinema --collection Cinemas --fields field1 --query "{ \"$expr\": {\"$gt\": [ \"$createdAt\" , {\"$dateSubtract\": {\"startDate\": \"$$NOW\", \"unit\": \"day\", \"amount\": 1}}]}}" --out output.csv

Obviously you’ll need to update the query to suit your needs as I just took 1 day off the current date

@John_Sewell command you provided seems to be working but it shows 0 reports exported.

So what are you running now? How did you update what i gave as an example to suit your needs?

@John_Sewell I just updated the collection fields and other attributes query part i haven’t made any change it ran without errors

Think I had a typo, use $$NOW and not $NOW.

This worked locally for some data I put into a test collection:

mongoexport --db Export --collection Test --fields myDate --query "{\"$expr\": {\"$gt\": [ \"$myDate\" , {\"$dateSubtract\": {\"startDate\": \"$$NOW\", \"unit\": \"day\", \"amount\": 3}}]}}"

yeah tried with that too but still 0 reports exported . are you running this in linux termnal @John_Sewell

Nope, running on a windows command, you’ll need to use appropriate escaping for a *nix shell.

I swapped to cygwin I had installed and this worked:

mongoexport --db Export --collection Test --fields myDate --query "{\"\$expr\": {\"\$gt\": [ \"\$myDate\" , {\"\$dateSubtract\": {\"startDate\": \"\$\$NOW\", \"unit\": \"day\", \"amount\": 3}}]}}"

Note the escaped $ symbols as I think using double quotes, the shell is interpreting them as replacement variables or some such.

Anyway thanks @John_Sewell still not able to proceed it shows feature not supported in putty terminal linux

That’s strange, can you share a screenshot of the error, blanking out any sensitive information?

@John_Sewell after executing the command it shows connected to mongodb and then shows Failed: feature not supported.

What version of the server / client tools are you using?

I am not sure of this but it looks like one of the $ is dropped whenever the command is cut-n-pasted.

What is published by John contains 2 $ but what is shared by Teo has only one $.

Teo, make sure you manually add the second $ so that you execute with $$NOW rather than $NOW.

server 3.x and mongoexport client running on 4.2.24

Ahhh, that’s a pretty ancient server! If you embed the date/time in the query as opposed to trying to use the $$now stuff to recalculate the date range does it work?

Can you try running the query you’re passing in against the server directly via mongo shell or compass etc?

You could try this:

 mongoexport --db Export --collection Test --fields myDate --query "{\"myDate\":{\"\$gt\":{\"\$date\":\"2023-08-28T00:00:00Z\"}}}"

And have the calling script create the date/time string and embed in the query as a variable?

No still it shows no reports exported and in compass when i give find it doesn’t return anything @John_Sewell