Squall Lionheart
Squall Lionheart

Reputation: 35

MongoDB Export Query to CSV using MongoExport

I really need help with the following problem with mongoexport :

  1. First of all the query is success with the following filter:

         db.payment_billers.find({
         createdDate : {
             $gte : ISODate("2022-04-01T00:00:00.000+07:00"),
             $lt : ISODate("2022-05-01T00:00:00.000+07:00")
          }
        })
    
  2. Then I tried to export the result to csv using this line:

     mongoexport --db=sigmob --collection=payment_billers
     --query='{createdDate : {$gte : ISODate("2022-04-01T00:00:00.000+07:00"),$lt : ISODate("2022-05-01T00:00:00.000+07:00")}}' --type=csv
     --fields=_id,accountSource.account.number,createdDate  --out=D:/download/220430/payment_billers.csv
    

Resulting :

2022-05-11T14:24:54.092+0700    error parsing command line options: error parsing positional arguments: provide only one MongoDB connection string. Connection strings must begin with mongodb:// or mongodb+srv:// schemes
2022-05-11T14:24:54.093+0700    try 'mongoexport --help' for more information

I tried the script without using the query and works great. Really need help to correct the script, and thank you guys for the help and attention.

Upvotes: 1

Views: 1785

Answers (1)

user14967413
user14967413

Reputation: 1416

This error arises when there is some redundant text in the end of the command (mongoexport treats it as connection string, hence the message). It may happen because your shell doesn't interpret your command in the way you expect.

What type of shell do you use? It seems you may use Windows cmd, which doesn't support single quotes. You should rather use double quotes for --query argument and escape double quotes inside the query with """ (read more about this escaping rule here):

--query="{"""createdDate""" : {"""$gte""" : ISODate("""2022-04-01T00:00:00.000+07:00"""),"""$lt""" : ISODate("""2022-05-01T00:00:00.000+07:00""")}}"

Note that I use double quotes also for field name createdDate and operators $gte and $lt, which is mandatory in mongoexport (see docs).

In you are using Linux bash your command should work properly after just enclosing field name and operators in simple double quotes.

--query='{"createdDate" : {"$gte" : ISODate("2022-04-01T00:00:00.000+07:00"),"$lt" : ISODate("2022-05-01T00:00:00.000+07:00")}}'

Upvotes: 1

Related Questions