0

Hi everybody in a previous post ( ListCollections with autorizedcollection ) I asked how to retrieve the name of the collections available in MongoDb'database. Finally the solution is much easier as expected and is :

            var db = mongoUser.GetDatabase(databaseName);
           
            var command = new CommandDocument { { "listCollections", 1 }, { "authorizedCollections", true },  {"nameOnly", true }};
            var result = db.RunCommand<BsonDocument>(command);

the problem is I am not used to work with BsonDocument. I could cast the "result" into a BsonDocument or onto a string and then cut until I found the name of the solution. But it is the wrong way. Could someone help me for this case ?

Magus
  • 57
  • 7
  • take a look at https://stackoverflow.com/questions/9478613/how-to-deserialize-a-bsondocument-object-back-to-class – COLD TOLD Feb 08 '21 at 17:10
  • Someone gived the response here : https://stackoverflow.com/questions/40338948/c-sharp-mongo-driver-imongodatabase-runcommand-to-get-database-stats – Magus Feb 09 '21 at 09:05

1 Answers1

0

you should not use CommandDocument since it's a Legacy API and you use not legacy code path in all other places. I think BsonDocument is the expected way to work with RunCommand. You can play with the above Deserialization suggestions but this will be just a step after RunCommand. So I would expect something like this:

  var command = new BsonDocument { { "listCollections", 1 }, { "authorizedCollections", true }, { "nameOnly", true } };
  var result = db.RunCommand<BsonDocument>(command);
  // any steps you want to process result including possible deserialization into typed view
dododo
  • 3,872
  • 1
  • 14
  • 37