簡體   English   中英

如何使用c#驅動程序復制mongo數據庫及其所有集合?

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

我正在嘗試使用c#驅動程序使用copydb命令復制mongo數據庫。 但這只是創建沒有任何集合的目標數據庫。 當我直接使用mongo shell運行命令時,它可以正常工作。

這些是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);

它不復制db1的集合。

這些是我碰到mongo shell並正常工作的命令:

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

我想念什么?

嘗試以下操作:

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

下一個代碼有效。 這是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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM