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 ?
CodePudding user response:
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.
CodePudding user response:
I tried to backup a collection using mongodump but didn't work, I don't know why, then I changed and used mongoexport and mongoimport like this, and it works fine.
mongoexport --uri="mongodb://host:port/fooDatabase" --collection=fooCollection --out=fooCollection.json
mongoimport --uri="mongodb://host:port/fooDatabase" --collection=newFooCollection --file=fooCollection.json --mode=upsert