1

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#

2
  • so what is db? Please try harder to ask a question I can answer without reading your mind. Commented May 1, 2014 at 11:20
  • is database a type you can reference? Commented May 1, 2014 at 11:22

2 Answers 2

1

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 );
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.