0

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

Kirbytech
  • 634
  • 1
  • 5
  • 18
  • What version of MongoDB are you using? The `--type` flag was added in v3 so this message "unknown option type" implies you are using a version < 3.0. If so, then the pre 3.0 equivalent of `--type=csv` is: `--csv`. – glytching Jan 23 '18 at 14:13
  • @glytching I am running V2.6 so I changed the one thing you mentioned and I got the error `Error parsing command line: too many positional options` – Kirbytech Jan 23 '18 at 17:02

1 Answers1

1

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.

Community
  • 1
  • 1
glytching
  • 44,936
  • 9
  • 114
  • 120
  • Thanks is it possible to include all fields quickly instead of me putting in every field manually? – Kirbytech Jan 23 '18 at 17:16
  • If you can accept JSON output then remove `--csv` and you won't need `--fields`. But if you **must** have csv output then you are forced to tell MongoDB _which_ fields you are interested in because MongoDB is schema-less whereas CSV has a fixed columnar layout so MongoDB needs to be told something about your desired CSV format. I'm not aware of a shortcut meaning _all fields_. You'd likely have to run something to [gather all field names](https://stackoverflow.com/questions/2298870/mongodb-get-names-of-all-keys-in-collection) and then update your mongoexport command. – glytching Jan 23 '18 at 17:27