来自 DOS 或 Windows 的 MongoDB 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12592305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:51:33  来源:igfitidea点击:

MongoDB commands from DOS or Windows

mongodbmongodb-.net-driver

提问by iefpw

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?

我正在尝试从 C# 程序自动将 4 个集合从一个 Mongo 数据库复制到同一台机器上的另一个数据库。我怎么做?MongoDB C# 驱动程序中是否有简单的副本收集/数据库命令?还是我必须先输入./mongo? 如果是这样,我如何在 MS-DOS 命令行窗口中执行此操作?喜欢./mongo -copycollection from to

回答by yakxxx

Use mongodump, Type:

使用 mongodump,输入:

./mongodump --db your_db_name --collection collection_name  

and then mongorestore:

然后 mongorestore:

./mongorestore --db=new_db_name

Read more: mongodumpand mongorestore

阅读更多:mongodumpmongorestore

回答by mongotop

you can use mongodump & mongorestore

你可以使用mongodump 和 mongorestore

1-> back up a single database
mongodump -h localhost -d database_name -o C:\DestinationFolder(backup to a DestinationFolder )

1-> 备份单个数据库
mongodump -h localhost -d database_name -o C:\DestinationFolder(备份到 DestinationFolder )

2-> Restore the Database

2-> 恢复数据库

mongorestore -h localhost C:\DestinationFolder(Restor from the DestinationFolder )

mongorestore -h localhost C:\DestinationFolder(从 DestinationFolder 恢复)

or

或者

3-> you ca backup and restor a single collection at a time

3-> 您可以一次备份和恢复一个集合

back up a single collection

备份单个集合

mongodump -h localhost -d database_name -c Collection_name -o C:\Dest_SingleCollBkp

4->Restore a single Collection

4->恢复单个集合

mongorestore -h localhost C:\Dest_SingleCollBkp

or

或者

5-> you can copy a single collection at the time

5->您可以一次复制单个集合

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

6->复制整个数据库

db.copyDatabase('from_database_name', 'to_databasename', 'from_hostname')