C# Mongo 运行命令异步

标签 c# mongodb

是否可以运行

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/

相关文章:

c# - 为什么我可以在 Linq to XML 查询中使用 (string) 而不是 "as string"?

c# - 在没有主键的实体中使用表(使用实体代码优先迁移)

node.js - 防止字段在初始设置后更新

windows - 为什么 MongoDB 位置运算符 ($[]) 在 Windows 计算机上失败,但在 Mac 上工作?

c# - 如何从 Outlook 插件获取 Gmail threadid(使用 Google Outlook Sync 时)?

c# - 具有相同动词和不同路由的 Azure API App 多个操作会产生 swagger 错误

c# - 在 C# 中将产品版本和文件版本设置为不同的数字

mongodb - 用 Meteor 中的新 mongo 集合替换现有 mongo 集合的最简单方法

node.js - mongoose.model( 'Foo',FooSchema)与要求( './models/Foos')

mongodb - 蒙戈 : count the number of word occurrences in a set of documents