Hi @Denis_Lezgin,
I did some tests and these are my results:
- 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")
}
- 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