Mongorestore –gzip –archive restore only specific collection

Hi there.

I have MongoDB backup that was created in following way:

mongodump -h <host> -u <user> --password=<password> -d <db_name> --authenticationDatabase <db_name> --archive=<backup_name>.gz --gzip

When I’m restoring the backup using “mongorestore” all the DB content is being restored.
I’m looking for a way to restore only specific collection from the backup to a new or same DB.

I’ve tried to use --nsInclude, --nsFrom and --nsTo flags, but they aren’t helping.

Is there any possible way to achieve what I’ve described above?

Any help is highly appreciated!

Thanks in advance.

Denis

Hi @Denis_Lezgin and welcome to the community!
I don’t remember precisely how to do it, but have you tried in nsFrom and nsTo to give the full namespace to try to restore the specific collection (i.e. database.collection)?

If that doesn’t work, I will do some tests tonight to give you a precise answer.

Regards

Hi Fabio,

yes, i tried it, and still got mongorestore restoring all the collections.

If that doesn’t work, I will do some tests tonight to give you a precise answer.

thanks, i’m looking forward for your response.

Hi @Denis_Lezgin,
I did some tests and these are my results:

  1. I’ve created the following database with some collection (test1,test2,test3) to simulate your request:
MongoDB Enterprise myReplicaSet:PRIMARY> use databaseToDump
switched to db databaseToDump
MongoDB Enterprise myReplicaSet:PRIMARY> db.test1.insertOne({x:true})
{
        "acknowledged" : true,
        "insertedId" : ObjectId("66ac9d03f311ba9aa78dd26c")
}
MongoDB Enterprise myReplicaSet:PRIMARY> db.test2.insertOne({y:true})
{
        "acknowledged" : true,
        "insertedId" : ObjectId("66ac9d13f311ba9aa78dd26d")
}
MongoDB Enterprise myReplicaSet:PRIMARY> db.test3.insertOne({z:true})
{
        "acknowledged" : true,
        "insertedId" : ObjectId("66ac9d1ef311ba9aa78dd26e")
}

  1. Dumped the database as archive:
[root@replicatre bin]# ./mongodump -d databaseToDump --archive="databaseToDump.archive"
2024-08-02T10:49:02.819+0200    writing databaseToDump.test3 to archive 'databaseToDump.archive'
2024-08-02T10:49:02.819+0200    writing databaseToDump.test2 to archive 'databaseToDump.archive'
2024-08-02T10:49:02.824+0200    writing databaseToDump.test1 to archive 'databaseToDump.archive'
2024-08-02T10:49:02.825+0200    done dumping databaseToDump.test2 (1 document)
2024-08-02T10:49:02.825+0200    done dumping databaseToDump.test3 (1 document)
2024-08-02T10:49:02.826+0200    done dumping databaseToDump.test1 (1 document)

a. Restored the databaseToDump with all collection in another database (databaseRestored)

mongorestore --archive="databaseToDump.archive" --nsFrom="databaseToDump.*" --nsTo="databaseRestored.*"

b. Dumped the collection that i need from the databaseRestored (so test1):

mongodump -d databaseRestored -c test1 --archive="test1.archive"

c. Finally we can restore the specific collection where we want.

Another way:

a. Restore directly the collection test1 from databaseToDump to databaseRestored (or where you want), but it generate some “duplicate key error collection”, because it also tries to restore the other existing collections in the same source db:

mongorestore --archive="databaseToDump.archive" --nsFrom="databaseToDump.test1" --nsTo="databaseRestored.test1"

I hope I have been helpful in this way.
If I think of anything else, I will let you know.

Best Regards

2 Likes

Hi Fabio,

Thanks for your response.
I’ve tried that before I posted my issue.
One thing I didn’t mention, is that I use pretty old MongoDB version which is 4.4.

could that be a reason?

Hi @Denis_Lezgin,
No, it should work the same way.

Regards

@Denis_Lezgin , After restoring it to new database . You can also use $out aggregation operator , to restore to specified collection . You dont have to make a dump again

2 Likes