简体   繁体   English

c# MongoDB RunCommand

[英]c# MongoDB RunCommand

Can someone give me an example of the use of the RunCommand method that takes a string argument only (called CommandName) available in the MongoDB .NET driver?有人可以给我一个使用 RunCommand 方法的示例,该方法仅采用 MongoDB .NET 驱动程序中可用的字符串参数(称为 CommandName)? I know there is an overloaded RunCommand method that takes an object reference (I think a CommandDocument object) as an argument, but I'd rather not use that one.我知道有一个重载的 RunCommand 方法,它将对象引用(我认为是 CommandDocument 对象)作为参数,但我不想使用那个方法。

I'm having trouble getting the syntax right for CommandName.我在为 CommandName 获取正确的语法时遇到了麻烦。 Thanks in advance!提前致谢!

If you are using some recent version of the official C# driver, the "real" string based version you are referring to ( CommandResult RunCommand(string commandName) ) is only part of the legacy driver component (check the namespace).如果您使用的是最新版本的官方 C# 驱动程序,您所指的“真实”基于string的版本 ( CommandResult RunCommand(string commandName) ) 只是旧驱动程序组件的一部分(检查命名空间)。 I would hence not recommend using it.因此我不建议使用它。

The "official" interface currently looks like this: “官方”界面目前看起来是这样的:

TResult RunCommand<TResult>(Command<TResult> command, /* and some additional optional parameters */)

And since the C# driver heavily relies on implicit type conversions, there also is one from a string (and a BsonDocument ) to the corresponding sub types of Command<TResult> ( JsonCommand<TResult> and BsonDocumentCommand<TResult> ).由于 C# 驱动程序严重依赖于隐式类型转换,因此还有一个从string (和BsonDocument )到相应的Command<TResult>子类型( JsonCommand<TResult>BsonDocumentCommand<TResult> )。 So you can effectively pass a string to the above new RunCommand() method, too.因此,您也可以有效地将string传递给上述 new RunCommand()方法。

You can therefore write either one of the following lines both of which do the exact same thing:因此,您可以编写以下任一行,它们都执行完全相同的操作:

RunCommand<BsonDocument>("{count: \"collection_name\"}")
RunCommand<BsonDocument>(new BsonDocument("count", "collection_name"))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM