I'm trying to copy 4 collections from one Mongo database to another on the same machine from C# program automatically. How do I do that? Is there a simple copy collection/database command in MongoDB C# driver? or do I have to use the Mongo shell by first typing the ./mongo
? If so how do I do that inside a MS-DOS command line window? Like ./mongo -copycollection from to
?
you can use mongodump & mongorestore
1-> back up a single databasemongodump -h localhost -d database_name -o C:\DestinationFolder
(backup to a DestinationFolder )
2-> Restore the Database
mongorestore -h localhost C:\DestinationFolder
(Restor from the DestinationFolder )
or
3-> you ca backup and restor a single collection at a time
back up a single collection
mongodump -h localhost -d database_name -c Collection_name -o C:\Dest_SingleCollBkp
4->Restore a single Collection
mongorestore -h localhost C:\Dest_SingleCollBkp
or
5-> you can copy a single collection at the time
copy ->
use source_database;
var docs = db.source_collection.find({ accessed: {
'$gte': new Date(2012, 4, 1), '$lt': new Date(2012, 5, 1)
} });
past -> :)
use new_database;
//switched to db new_database
docs.forEach(function(doc) { db.new_collection.insert(doc) });
6-> copy the entire database
db.copyDatabase('from_database_name', 'to_databasename', 'from_hostname')
Use mongodump, Type:
./mongodump --db your_db_name --collection collection_name
and then mongorestore:
./mongorestore --db=new_db_name
Read more: mongodump and mongorestore
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With