是否可以运行
db.Users.createIndex({"FirstName" : 1, "LastName": 1});
在 RunCommandAsync 中还有可以通过命令完成的示例。
作为测试,我尝试运行它,但它遇到了异常
var indexCommand = new BsonDocumentCommand<BsonDocument>(new BsonDocument{
{"getIndexes", "Users"}
});
var results = await database.RunCommandAsync(indexCommand);
我知道我可以从 C# 驱动程序获取和创建索引,但是我想在那些想要通过 C# 构建索引的人和那些想直接在数据库上处理它的人之间保持脚本同步。
最佳答案
您或许可以尝试其他方法。特别是当您看到 RunCommandAsync 预期对 BsonDocument 执行的操作时。参见 Unit test for RunCommandAsync
假设您使用的是 mongodb 3.0,您可以考虑使用下面的代码作为替代方案吗?
using (var results = await context.Users.Indexes.ListAsync())
{
while (await results.MoveNextAsync())
{
foreach (var current in results.Current)
{
System.Diagnostics.Debug.WriteLine(current["name"].AsString);
}
}
}
关于C# Mongo 运行命令异步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31393329/