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

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/

Related

mongorestore of specific database from full dump - safe?

I must restore a single Mongodb database from a full backup of many databases
without touching the other databases:
e.g. $ mongorestore --gzip --drop --db ONEDB dbbackupfile
Will the restore command honour the --db ONEDB and ONLY drop the ONEDB database collection? Or will it drop them all?
[The documentation for mongorestore only says that --drop will drop all collections in the backup - it doesn't say what effect the --db option has on the --drop flag...]
Thank you in advance for your help.
I understand the fear of dropping all your databases,
With that being said the documentation clearly states:
drops the collections from the target database.
i recommend that if your experimenting with this command for the first time and you're not confident with it yet you should test it not on production but on your local first, or at the very least run it with --dryRun flag just to see if your getting the results you want.

how to copy a collection from one from another in robomongo

I have a collection named dashboard in one db and i want to copy that collection to another db using robomongo. How can i do this? I tried creating a new collection in 2nd db and tried copying but it failed. so somebody please help me
Another db - another connection. But Robomongo works only with one connection in one period of time. That is why it is impossible.
I suggest you to use mongoimport/mongoexoprt tools for your task. They comes with mongo, are located in same folder as mongod.exe and allows to move collections via databases, by exporting to and importing from a json file.
Code sample:
mongoexport --db testFrom --port portFrom --username userFrom --password passwordFrom --collection yourCollection --out test.json
mongoimport --db testTo --port portTo --username userTo --password passwordTo --collection yourCollection --file test.json

Can I restore metadata when restoring a MongoDB collection?

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.

How can I create a database in mongodb and import the data from a csv to its collection

I am not able to figure out,how to create a database in mongodb.
When I login in to the mongo console it shows me test database as default. What are the steps to create a new database?
In MongoDB, to create a database from within the Mongo shell you use
use mydatabasename
where mydatabasename is the name you want to give to the database. If you want to list the databases
show dbs
The shell quick reference documentation is pretty good.
For importing data from a CSV file, you can use mongoimport
Whenever we login into mongo without passing any parameter, say db or hostname, it will automatically connect you with test db.
In order to create DB and dump data from CSV you can use the following command :-
mongoimport --db nxtshow --collection movies --type csv --file ./mongo/movies.csv --fields id,imdb_url,release_date,title,video_release_date

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.