user3577947
user3577947

Reputation: 287

How to count collections in database c# and mongodb

Hello I need to count collections in my database with c#

if I use run command i wrote db.stats()'. but I don`t know how use this command in c#

Upvotes: 1

Views: 1170

Answers (2)

aks
aks

Reputation: 720

If all you need is the count of the collections in a specific database, most likely using following should work fine:

MongoDatabase mdb = XXX;
IEnumerable<string> collNames = _mdb.GetCollectionNames();

collNames will have all collection names in the database and the count of that is the number of collections under that database. Most likely you may want to filter that collection for system collections like system.users, system.profile etc.

Upvotes: 0

Neil Lunn
Neil Lunn

Reputation: 151112

Good thing that the mongo shell is a REPL. So evaluate the internals of the function there like this:

> db.stats
function (scale){
    return this.runCommand( { dbstats : 1 , scale : scale } );
}

So all you need now is the command invocation for c#

var commandDoc = {
    { "dbstats", 1 }
};

var commandResult = db.RunCommand( commandDoc );

Upvotes: 1

Related Questions