简体   繁体   English

如何使用c#驱动程序复制mongo数据库及其所有集合?

[英]How to copy mongo database with all it's collection using c# driver?

I am trying to copy a mongo database using copydb command using c# driver. 我正在尝试使用c#驱动程序使用copydb命令复制mongo数据库。 But it's just create the target db with no collection inside. 但这只是创建没有任何集合的目标数据库。 When i run the command directly with the mongo shell it work fine. 当我直接使用mongo shell运行命令时,它可以正常工作。

These is the c# code: 这些是C#代码:

var db = mongo.GetServer().GetDatabase("admin");
var command = new CommandDocument(new BsonElement("copydb", 1),
                                  new BsonElement("fromdb", "db1"),
                                  new BsonElement("todb", "db2")
                                 );
var result = db.RunCommand(command);

Its not copy the collections of db1. 它不复制db1的集合。

These is the command i run into mongo shell and work fine: 这些是我碰到mongo shell并正常工作的命令:

db.runCommand({copydb:1, fromdb:"db1", todb:"db2"})

What am i missing? 我想念什么?

Try following : 尝试以下操作:

var result = db.RunCommand(
            new CommandDocument(new BsonElement("copydb", 1),
                new BsonElement("fromhost", "localhost"),
                new BsonElement("fromdb", "sourcedb"),
                new BsonElement("todb", "targetdb")));

Next code works. 下一个代码有效。 It's C# MongoDB.Driver 2.0 这是C#MongoDB.Driver 2.0

var database = mongoClient.GetDatabase("admin");
var command = @"{ copydb: 1, fromhost: 'localhost', fromdb: 'from', todb: 'toDbName'}";
await database.RunCommandAsync<BsonDocument>(BsonDocument.Parse(command));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Mongo C#驱动程序的BSONClassMap将所有属性序列化和反序列化为String - How do I Serialize and DeSerialize all properties to String using Mongo C# Driver's BSONClassMap 如何在c#驱动程序中为mongo集合中的所有文本字段创建全文搜索? - How do you create a full text search for all text fields in a mongo collection in the c# driver? 在mono中使用c#驱动比较mongo集合的两个字段 - Comparing two fields of mongo collection using c# driver in mono 使用c#driver从mongo collection获取DateTime - Get DateTime from mongo collection using c# driver 如何在C#mongo驱动程序中设置复数集合名称? - How to set plural collection name in C# mongo driver? Mongo DB C#驱动程序-如何在集合中通过ID加入? - Mongo db c# driver - how to join by id in collection? 如何通过 DateTime 的 C# 驱动程序在 Mongo 集合中查找条目? - How to find an entry in a Mongo Collection through C# driver by DateTime? 如何在C#中使用mongo驱动程序按条件排序 - How to sort by condition using the mongo driver in c# 如何通过在 mongo C# 驱动程序中仅使用连接字符串来`ListCollections`? - How to `ListCollections` by using only connection string in mongo C# driver? 如何使用mongo db c#驱动程序执行“和”查询? - How to do “And” queries using mongo db c# driver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM