I am trying to get all the collection names from MongoDb server using C# code using db.GetCollectionNames() method.
What am I doing wrong? Or if there is an alternate way?
Hi Shweta,
Based on the GetCollectionNames() method, I assumed that you are using MongoDB .Net driver v1.x - currently at version v1.11.
You should be able to retrieve the collection names with a C# snippet code like below:
var client = new MongoClient("mongodb://<host>:<port>");
var server = client.GetServer();
var database = server.GetDatabase("databaseName");
foreach (var coll in database.GetCollectionNames())
{
Console.WriteLine(coll.ToString());
}
The above snippet was tested on MongoDB v3.2, MongoDB .Net driver v1.11, .Net v4.5.2
If you are still having a trouble listing collection, could you provide:
Do you have access control enabled?
If so, could you provide any MongoDB logs or if you are getting any error messages? perhaps you are connected using a database user that does not have the required access ?
Kind regards,
Wan.