来自C#的MongoDB db.runCommand() [英] MongoDB db.runCommand() from C#

查看:112
本文介绍了来自C#的MongoDB db.runCommand()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将C#与MongoDB官方驱动程序v2.2.4结合使用,我想在管理数据库上运行db.runCommand().

Hi I'm using C# with MongoDB Official driver v2.2.4 and I want to run db.runCommand() on the admin database.

到目前为止,我已经拥有了这个并且我能够连接到管理数据库,但是db.runCommand给了我这个错误"MongoDB.Bson.dll中发生了'System.FormatException'类型的未处理异常"附加信息:JSON阅读器期望值,但找到'db'."

So far i have this and i am able to connect to the admin database but db.runCommand is giving me this error "An unhandled exception of type 'System.FormatException' occurred in MongoDB.Bson.dll Additional information: JSON reader was expecting a value but found 'db'."

MongoClient client = new MongoClient();
database = client.GetDatabase("admin");
var collection = database.GetCollection<BsonDocument>("test");
var commandResult = database.RunCommand<string>(@"db.createCollection(test1)");

解决此测试后,我想从C#运行此命令,但我被卡住了.

After I resolve this test I want to run this command from C# but I am stuck.

db.runCommand( { addshard : "localhost:10001", name : "shard10001" } );

任何人都可以解决此问题,并为我提供良好的解释和示例.经过一番搜索后,我尝试使用此代码似乎更有意义,但仍然出现错误."其他信息:命令addshard失败:没有这样的命令:'addshard',错误的cmd:'{addshard:"192.168.1.4:27017",名称:"shard1"}'."

Any one can resolve this problem and provide me with a good explanation and example. After some search I have tried this code does seems to make more sense but still getting an error. "Additional information: Command addshard failed: no such command: 'addshard', bad cmd: '{ addshard: "192.168.1.4:27017", name: "shard1" }'."

请问我在做什么错的任何想法!谢谢.

Any ideas please of what I'm doing wrong! Thanks.

    var addShardCommand = new BsonDocument {
        { "addshard", "192.168.1.4:27017"},
        { "name", "shard1" }
    };
    var addShardResult = database.RunCommand<BsonDocument>(addShardCommand);

推荐答案

您需要检查mongodb中正确的命令是什么.就像某个时候,名称需要Document对象,而不仅仅是字符串.

You need to check what is the correct command in mongodb. like sometime name need Document object instead of just string.

我正在使用类似的东西.检查是否有帮助

I am using something like this. check if this help

var name = new BsonDocument { { "name", "regions" } };
var command = new BsonDocument { { "listCollections", 1 }, { "filter", name } };
var result = Database.RunCommand<BsonDocument>(command);
var k = result.ToJson();
            

这里的名称再次是我从本文档中找到的对象 https://docs.mongodb.com/manual/reference/command/listCollections/

Here name is again object which I found from this documentation https://docs.mongodb.com/manual/reference/command/listCollections/

您可以从这里获得更多帮助 https://zetcode.com/csharp/mongodb/

Some more help you can take from here https://zetcode.com/csharp/mongodb/

这篇关于来自C#的MongoDB db.runCommand()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆