Can I restore metadata when restoring a MongoDB collection? - mongodb

When I restore a collection with the following command:
mongorestore --db mydb --drop --collection mycollection --batchSize=100 mycollection.bson
as the original collection is dropped, the indexes are lost. I can see that there is also a mycollection.metadata.json file which contains indexes of this collection, but I cannot find in the documentation how this file can be restored.
All I've found is how to restore an entire database, which restores all collections with metadata from a directory. However, I want to restore only a single collection. How do I do that?
Note: I am using mongo version 3.0.7

You don't need to do something specific to restore metadata.
mongorestore does this for you.
When you restore collection:
mongorestore --collection mycollection --db mydb mycollection.bson
mongorestore checks directory where mycollection.bson exist for mycollection.metadata.json file. Just keep metadata file in the same directory as collection.

Related

Restore Done but I dont see any database

I run this
mongorestore --db dbName
and I got
the --db and --collection args should only be used when restoring from
a BSON file. Other uses are deprecated and will not exist in the
future; use
--nsInclude instead
so I used
mongorestore --nsInclude 'dbName.*'
and I got
2018-01-06T21:28:02.106+0200 using default 'dump' directory
2018-01-06T21:28:02.142+0200 preparing collections to restore from
2018-01-06T21:28:02.147+0200 done
but I don't see any db of dbName that created.
I found the answer here Mongorestore of a db causing me trouble
mongorestore --db you_db_name --drop dump/you_db_name
Hope this helps.

mongodump --oplog and per database restore

I need to be able to restore a single database, even a single collection from a backup. Since mongodump --oplog only applies to a full instance (Replica Set), I made the following procedure to filter only the entries from the db that I want to restore from the oplog.bson generated by the --oplog option.
Could someone tell me if this is correct or if I'm missing something?
First I restore only the db (the db is test in this example) that I need
mongorestore -d test dump/test
Restore into a random collection the oplog.bson file generated by the --oplog option
mongorestore -d oplog -c oplog dump/oplog.bson
Dump from the restored oplog.bson collection only the documents that refer to the db that I'm restoring
mongodump -d oplog -c oplog -q "{ns:/^test[.]/}" -o oplog
Restore with the --oplogReplay option using the last dump with filtered operations
mongorestore --oplogReplay oplog/oplog
Finally I drop the temporary oplog collection.
mongo --eval "db.getSisterDB('oplog').dropDatabase()"
Thanks in advance!
Basically, that's one way to do it.. Or you can do extra export after dumping specific database.
mongoexport -d local -c oplog.rs --query="{ns:/^test[.]/}" -o oplog.dump
2017-10-10T11:51:50.780+0300 connected to: localhost
2017-10-10T11:51:51.737+0300 local.oplog.rs 0
2017-10-10T11:51:51.938+0300 local.oplog.rs 3
2017-10-10T11:51:51.938+0300 exported 3 records
And you have now direct json dump file what you can read back

Move a collection from one DB to another DB within the same MongoDB instance

I have many collections in database A. I want to copy some of them to database B in the same MongoDB.
I tried to copy whole database using db.copyDatabase('A', 'B'), but it has more than 300 GB data. It will take ages to copy and I just want to copy a few collections from database A to database B.
Does anyone know how can I do it?
You can try it using mongo shell. you can copy collection from one db to another db using renameCollection. the renameCollection can run from admin database so first need to switch to admin db.
so ca follow bellow steps in mongo shell:
step-1: run this comment use admin
step-2: run bellow comment
db.runCommand({renameCollection:"sourcedb.sourceCollection",to:"targetdb.tragetCollection"})
for example:
use admin
db.runCommand({renameCollection:"funnel.countries",to:"test.countries"})
copied countries collection from funnel db to test db.
In background MongoDB will create dump for source collection and restore the dump automatically to target db collection
Use mongodump to dump the collections:
mongodump --db A --collection coll --out yourbackupdir
and then import the collections using mongorestore:
mongorestore --db B --collection coll yourbackupdir/

drop whole database within a single command of restoring the dump of mongodb

I am trying to restore the directory dump of mongodb.
i am doing
mongorestore --db mydb --drop path/to/mydb/dump
But both does not able to restore the state of my dump.
Any new records are visible even after restoring the db.
But no error is shown on console.
I didn't see an answer and I had the same question today.
You can drop the database before with:
use <db>
db.dropDatabase()
Or you can only drop the collection with:
db.<collection>.drop()
The problem with your command could be that something misses, like the database which you authenticate against or the user or maybe another thing.
In my setup this works
mongorestore --username=<user> --db=<database> --authenticationDatabase=<database> --dir=<dumpdir> --drop
If your dump was zipped beforehand, you can add the --gzip flag in the end.
You can find all that in the documentation for dropping a database or in the documentation of dropping a whole database. But please be cautious with it.

mongodump ignore some specified collections

I was trying to backup my mongo database on the product sever.and then restore then back to the staging server.
and here comes some problem, there are a lot of collections in db, I want to igonre some collections that I don't want to restore on staging server.
I can approach this by dumpping the staging db, dumpping the producting db, and then restore the prodct to staging useing --drop option. and restore the specified collections in staging db. uh..it's really bad.
1. dump producting db
mongodump --host product-server-host --username abcd --password bcda -d db -o pruduct-dump-dir
2. dump staging db
mongodump --host staging-server-host --username abcd --password bcda -d db -o staging -dump-dir
3. restore all collection, then restore the collection back
restore pruduct-dump-dir to staging server
mongorestore --host staging-server-host --username abcd --password bcda --drop pruduct-dump-dir
mongorestore --host staging-server-host --username abcd --password bcda --drop --collection coll pruducting-dump-dir
Is there any option like ignore-collection when I'm dumpping?
any suggestion will be appreciated :3
Now available from version 3.0.0
--excludeCollection <collection_name>
--excludeCollectionsWithPrefix <collection_prefix>
Repeat to exclude more than 1
Checkout the documentation
mongodump --db test --excludeCollection=users --excludeCollection=salaries
You can add --collection COLLECTION_NAME to dump the collection you need. By default, if you do not specify a collection to dump from a database, MongoDump will dump all collections in that database.
As of Mongo 3.4, you can now specify an --nsExclude <namespace pattern> option when restoring from a Mongo database dump, which will exclude the specified namespaces from the restore operation. This is particularly useful when the mongodump operation has already occurred.
Official documentation here: https://docs.mongodb.com/manual/reference/program/mongorestore/#cmdoption-nsexclude
You can exclude multiple collections with wildcards:
mongorestore --db test --nsExclude 'test.*_tmp'
Or alternatively, specifying multiple --nsExclude options work as well:
mongorestore --db test --nsExclude 'test.collection1' --nsExclude 'test.collection2'
I had to do the same while backing up a mongo db. If you use python (or any other language), you can use similar approach as well. After doing the mongodump, you simple have to remove the unwanted collection's bson & the metadata.json files.
import os
EXCLUDE_COLLECTIONS = ['collection_1', 'collection_2']
db_dump_path = "/Path/to/mongodump"
db_name = "name_of_db"
for collection_name in EXCLUDE_COLLECTIONS:
bson_file_path = os.path.join(db_dump_path, db_name, '{}.bson'.format(collection_name)
meta_file_path = os.path.join(db_dump_path, db_name, '{}.metadata.json'.format(collection_name)
if os.path.exists(bson_file_path) and os.path.exists(meta_file_path):
os.remove(bson_file_path)
os.remove(meta_file_path)