MongoDB commands from DOS or Windows

倖福魔咒の 提交于 2019-12-12 10:42:57

问题


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?


回答1:


Use mongodump, Type:

./mongodump --db your_db_name --collection collection_name  

and then mongorestore:

./mongorestore --db=new_db_name

Read more: mongodump and mongorestore




回答2:


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')


来源:https://stackoverflow.com/questions/12592305/mongodb-commands-from-dos-or-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!