how to restore a collection or collections in mongodb from an archive file?
Asked Answered
H

3

6

I have created a dump in an archive file by the following command :

mongodump --archive=newcollection.1.archive --db neel --collection newcollection

While restoring with the mongorestore i am getting an error :the --db and --collection args should only be used when restoring from a BSON file.

I got the syntax from [https://docs.mongodb.com/manual/reference/program/mongorestore/#examples][1] .

I fired the following command :

mongorestore --archive=newcollection.1.archive  --db backup2.
Hildegardhildegarde answered 13/1, 2017 at 7:24 Comment(7)
you can't use the --db param when restoring from archive, just use mongorestore --archive=newcollection.1.archiveAdaline
then i will get duplicate key errorHildegardhildegarde
And moreover i want to restore data in some specified databaseHildegardhildegarde
then you can't use --archive flag. You should do a classic mongodump and work with the BSON filesAdaline
You mean to say i can't restore into some other database from archive file?Hildegardhildegarde
as of mongodb 3.4, yes. An issue has been openend for this, see jira.mongodb.org/browse/TOOLS-1073Adaline
oh yes that's an issue with mongo 3.4Hildegardhildegarde
P
9

use --nsFrom and --nsTo

see example :

mongodump -h foo.com:27017 --db source_db_name --gzip --archive | mongorestore --drop -vvvvvv -h bar.com:27017 --nsFrom source_db_name.collection_name --nsTo destination_db_name.* --gzip --archive

the format for nsFrom and nsTo is database_name.collection_name, and you can use wildcards ( like i did for destination_db_name.* ) for the same

more info

Premonitory answered 28/12, 2017 at 20:56 Comment(1)
The example from the docs was giving me the E11000 duplicate key error collection: db.collection index: _id_ dup key error. Your answer gave me the hint to remove quotes from --nsFrom and --nsTo, and the error went away.Jurkoic
C
2
mongorestore  --archive=<filename>.archive
Cupcake answered 12/2, 2022 at 19:29 Comment(0)
T
0

when you want to restore a mongodb archived database go to file path and write

mongorestore --archive=filename.archive

and then press enter it will restore all data of from archive file to your local database.

Tymbal answered 9/12, 2022 at 6:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.