mongodbmongodumpmongorestoremongodb-tools

Mongorestore a single collection through stdin output


I would like to copy a MongoDB collection to another database in a single command line using the standard output.
I don't want to dump the entire database.
The target collection will have a different name from the copy.

Here is my command :

> mongodump -h=HOST -d=db1 -c=from_coll --archive --gzip | mongorestore -h=HOST --nsInclude=db2.target_coll --archive --gzip

  writing db1.from_coll to an archive on stdout
  preparing collections to restore from
  done dumping db1.from_coll (100000 documents)
  0 document(s) restored successfully. 0 document(s) failed to restore.

The mongodump part seems to work well.
But we have (0 document(s) restored successfully)
Why the mongorestore doesn't copy the data in the target collection as expected ?


Solution

  • The --nsInclude option filters the input data. Since the bson data being passed from mongodump does not include any documents from db2.target_coll, nothing is restored.

    Use the --nsFrom and --nsTo to rename a collection with mongorestore.