【发布时间】:2017-10-17 15:30:14
【问题描述】:
谁能给我一个使用 RunCommand 方法的示例,该方法仅接受 MongoDB .NET 驱动程序中可用的字符串参数(称为 CommandName)?我知道有一个重载的 RunCommand 方法将对象引用(我认为是 CommandDocument 对象)作为参数,但我宁愿不使用那个。
我无法正确获取 CommandName 的语法。提前致谢!
【问题讨论】:
谁能给我一个使用 RunCommand 方法的示例,该方法仅接受 MongoDB .NET 驱动程序中可用的字符串参数(称为 CommandName)?我知道有一个重载的 RunCommand 方法将对象引用(我认为是 CommandDocument 对象)作为参数,但我宁愿不使用那个。
我无法正确获取 CommandName 的语法。提前致谢!
【问题讨论】:
如果您使用的是官方 C# 驱动程序的某个最新版本,那么您所指的“真正的”基于 string 的版本 (CommandResult RunCommand(string commandName)) 只是旧版驱动程序组件的一部分(检查命名空间)。因此我不建议使用它。
“官方”界面目前是这样的:
TResult RunCommand<TResult>(Command<TResult> command, /* and some additional optional parameters */)
由于 C# 驱动程序严重依赖隐式类型转换,因此还有一个从 string(和 BsonDocument)到 Command<TResult>(JsonCommand<TResult> 和 BsonDocumentCommand<TResult>)的相应子类型。因此,您也可以有效地将string 传递给上述新的RunCommand() 方法。
因此,您可以编写以下任一行,它们的作用完全相同:
RunCommand<BsonDocument>("{count: \"collection_name\"}")
RunCommand<BsonDocument>(new BsonDocument("count", "collection_name"))
【讨论】: