Mongoexport syntax on powershell

Hello, I am very new to MongoDB. google this mongoexport syntax but couldn’t get it work on powershell. Not sure what I did wrong. thanks in advance. My server is windows 2016 , powershell 5.1.
mongoexport --collection=<collection_name> --db=<db_name> --out=D:\out.json --query= ‘{“createdAt”: {"$lt": {"$date": new Date(1597261765739)}}}’

I got this error “too many positional arguments: [{ createdAt: {: {: new Date(1597261765)}}}]”.
I also tried
‘{“createdAt”: {"$lt": {"$date": “2020-02-28T00:00:00.000Z”}}}’ and
“{“createdAt”:{”$gte": new Date(1597261765739}}}"… they all end up with same error

The createdAt has value like this in the collection
“createdAt” : ISODate(“2020-04-13T23:01:01.560+0000”)

Not sure what I did wrong here.

Not sure but ot looks like you have an extra space after –query= and before the query itself.

Thanks for the reply. I made some progress. look like previous error is due to the character of the quote. error from copy/paste. After modify it, it executed but 0 records returned. I tried:
–query="{‘createdAt’: {’$lt’: {’$date’: ‘2020-08-13T23:01:01.560+0000’}}}"
–query="{‘createdAt’: {’$lt’: {’$date’: ‘2020-07-28T00:00:00.000Z’}}}"
–query="{‘createdAt’: {’$lt’: {’$date’: new Date(1597261765739)}}}"

  •   + CategoryInfo          : NotSpecified: (2020-08-19T13:3...d to: localhost:String) [], RemoteException
      + FullyQualifiedErrorId : NativeCommandError

2020-08-19T13:35:17.694-0600 exported 0 records

I am sure there are some records before July.

Quotes are indeed an issue from time to time. In particular when cut-n-pasting from a web page since html or UTF introduced fancy quotes, see

Try with (I tried to make sure that it is ' and ")

--query="{'createdAt': {'$lt': {'$date': '2020-08-13T23:01:01.560+0000'}}}"

or (you usually what the single quote outside to make sure the shell does not do any magic)

--query='{"createdAt": {"$lt": {"$date": "2020-08-13T23:01:01.560+0000"}}}'

thanks Steve. Looks like the first option

–query="{‘createdAt’: {’$lt’: {’$date’: ‘2020-08-13T23:01:01.560+0000’}}}"

I got “exported 0 records”.
but got problem with second one

–query=’{“createdAt”: {"$lt": {"$date": “2020-08-13T23:01:01.560+0000”}}}’

return with error

.\mongoexport : 2020-08-24T15:27:10.330-0600 error validating settings: query ‘[123 99 114 101 97 116 101 100 65 116 58 32 123 36 103 116 58 32 123 36 100 97 116 101 58 50
48 50 48 45 48 56 45 49 56 84 48 48 58 48 48 58 48 48 46 53 54 48 43 48 48 48 48 32 125 125 125]’ is not valid JSON: invalid character ‘-’ after object key:value pair…

I will play around to see what went wrong…

ok, finally. I made some progress. It is the problem with my Powershell code. I tested the mongoexport with command prompt with --query="{‘createdAt’: {’$lt’: {’$date’: ‘2020-08-13T23:01:01.560+0000’}}}", the collection was successful exported.

I will work on the powershell script. thank you for your help.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.