C # Mongo RunCommandAsync

Is it possible to run

db.Users.createIndex({"FirstName" : 1, "LastName": 1});

from RunCommandAsync there are also examples of what can be done from the team.

As a test, I tried to run this, but through an exception

var indexCommand = new BsonDocumentCommand<BsonDocument>(new BsonDocument{
        {"getIndexes", "Users"}
});
var results = await database.RunCommandAsync(indexCommand);

I know that I can receive and create indexes from C # drivers, however, I would like to synchronize the script between those who want to create indexes through C # and those who want to process it directly in the database.

+2
source share
1 answer

You can probably try an alternative method. Especially if you see what is expected from RunCommandAsync with BsonDocument. See Unit test for RunCommandAsync

Assuming you are using mongodb 3.0, can you consider the code below an alternative?

    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);
            }
        }
    }
0
source

Source: https://habr.com/ru/post/1613870/


All Articles