Search code examples
node.jsmongodbmongoosedatabase-backupsmongoexport

Query using mongoexport for req.user.id


I am trying to export a database called asset-management and all collections where req.user.id except the users collection. I can't seem to get the below to work.

mongoexport --db asset-management --collection * --type=csv --query '{"author.id": req.user.id}' --out userbackup.csv

I get this error message:

Error parsing command line: unknown option type


Solution

  • In Mongo 2.6, this should work:

    mongoexport --db asset-management --csv --query  "{'author.id': 'req.user.id'}" --fields \"fieldNameA,fieldNameB\" --out userbackup.csv
    

    The differences between this and the mongoexport command in your question are:

    1. Swapped " and ' in the --query parameter, to address "Error parsing command line: too many positional options"

    2. Removed --collection * this looks like like it is intended to mean all collection in which case it is redundant since that's the default behaviour. In your question you stated: "I am trying to export a database called asset-management and all collections where req.user.id except the users collection", this is not possible when using mongoexport ... you can export a specific collection or all collections but you cannot blacklist a single collection

    3. Added --fields because according to the docs ...

      If you specify --csv, then you must also use either the --fields or the --fieldFile option to declare the fields to export from the collection.