Skip to content

MongoDB Export and Import

prohaska edited this page Apr 15, 2013 · 1 revision

Missing header

Mongo Export

  • Export a collection to a JSON coded file.
  • Runs a query on the collection to retrieve the documents.
  • Can include query document from the command line to select documents.
  • MongoDB optimizes the index selection.

Mongo Import

  • Import JSON coded documents from a file to a collection.
  • Optionally drop the collection.
  • Can insert or update the documents.
  • Each insert or update is a separate operation.
  • Duplicate key errors are ignored. The import code does not call getLastError after inserts, and does not ever call getPrevError.
  • Duplicate key errors are ignored even when stopOnError is set.
  • Running with dbpath causes the mongoimport process to do the import directly to the database files.
  • If mongoimport dbpath finds the env locked, it complains (good) but exits with a 0 exit code (bad).

Tests

  • Located in jstests/tools/exportimport*.js.
  • ToolTest requires /data/db for some reason.

Possible changes

  • Make mongoimport transactional. All or no documents are imported.
  • Make mongoimport check for errors, including duplicate key errors.
  • Errors should cause mongoimport to exit with a non-zero exit code.
    • For example, mongoimport with a locked data directory should exit with a non-zero exit code.