[英]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.