Can mongoexport accept JavaScript file extension (.js) for the --queryFile option?

29 Views Asked by At

Wonder to know if mongoexport --queryFile can accept js files? If only json then how can cater this query db.CustomerDocument.aggregate({ $project: { "_id": 1, "Country": 1, "date": { $toDate: "$CustomerOnlineActiveDate" } } })

The following command runs with errors, due to --queryFile value.

mongoexport --uri "mongodb+srv://..." --username myUser --password myPassword  --db CustomerDb --collection Customer --fields _id,Country,date --type csv --queryFile c:\test\FromMongo\AccountConversion.json  --out c:\test\Mini.csv 

In this mongoexport command if I just delete the --queryFile and its assigned path, it works but the case is that I need the following query be executed.

The text in query file is as below. On mongo shell it can be executed with no issues.

db.CustomerDocument.aggregate({
    $project: {
        "_id": 1,
        "Country": 1,
        "date": {
            $toDate: "$CustomerOnlineActiveDate"
        }
    }
})

the error I encounter is: []' is not valid JSON: invalid character 'd' looking for beginning of value.

As an alternative to this, I created a .json file with the follwoing content:

{"$project":{"_id":1,"Country":1,"date":{"$toDate":"$CustomerOnlineActiveDate"}}}

and changed accordingly the path to this .json file but still got the following error:

the error I encounter is: []' is not valid JSON: invalid character '\' looking for beginning of object key string.

1

There are 1 best solutions below

0
Joe On

You are passing a projection where a query filter is expected.

From the docs:

--queryFile=

Specifies the path to a file containing a JSON document as a query filter that limits the documents included in the output of mongodump. --queryFile enables you to create query filters that are too large to fit in your terminal's buffer.

The query filter must be a JSON document, containing query operators.

You might be able to get what you are looking for in 2 steps:

  • run the aggregation with the projection and use a $out stage to store the result in a temporary collection
  • mongoexport from the temporary collection