skip to Main Content

I’ve tryed running a raw mongo command from C# this is the command that I’m interested to run in C#

db.getUser("MyUser")

I’ve tried

    public static async Task GetUserInfoAsync(this IMongoDatabase database, string username, string databaseName)
    {
        try
        {
            BsonDocument document = new BsonDocument
            {
                {"usersInfo", new BsonDocument
                    {
                        { "user", username},
                        { "db", databaseName}
                    }
                }
            };

            BsonDocumentCommand<BsonDocument> command = new BsonDocumentCommand<BsonDocument>(document);

            var t = await database.RunCommandAsync(command);
        }
        catch (Exception ex)
        {
            throw;
        }
    }

What I get is

ok=1 , users = []

no matter if the users exists or not

2

Answers


  1. Check what you expect to see in the shell, for now it looks like the filter data (about db?) you provide is incorrect. Do you see any result without db filter? See for details. There is no special logic from .net driver here, all what it does is fully the same as db.getUsers does in the shell. You may see it in the shell source code:

    MongoDB Enterprise mongos> db.getUsers
    function(args) {
        var cmdObj = {usersInfo: 1};
        Object.extend(cmdObj, args);
        var res = this.runCommand(cmdObj);
        if (!res.ok) {
            var authSchemaIncompatibleCode = 69;
            if (res.code == authSchemaIncompatibleCode ||
                (res.code == null && res.errmsg == "no such cmd: usersInfo")) {
                // Working with 2.4 schema user data
                return this.system.users.find({}).toArray();
            }
    
            throw _getErrorWithCode(res, res.errmsg);
        }
    
        return res.users;
    }
    
    Login or Signup to reply.
  2. You can try this code which uses the command usersInfo.

    var cmdStr = "{ usersInfo: { user: 'a_user_name', db: 'some_db_name' } }";
    var cmd = BsonDocument.Parse(cmdStr);
    var result = new MongoClient("mongodb://127.0.0.1:27017").GetDatabase("test").RunCommand<BsonDocument>(cmd);
    Console.WriteLine(result);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search