Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB commands from DOS or Windows

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?

like image 931
iefpw Avatar asked Sep 25 '12 22:09

iefpw


2 Answers

you can use mongodump & mongorestore

1-> back up a single database
mongodump -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')
like image 114
mongotop Avatar answered Oct 13 '22 01:10

mongotop


Use mongodump, Type:

./mongodump --db your_db_name --collection collection_name  

and then mongorestore:

./mongorestore --db=new_db_name

Read more: mongodump and mongorestore

like image 36
yakxxx Avatar answered Oct 13 '22 01:10

yakxxx