How to list the collections of a database using a command

84 views
Skip to first unread message

The JMCNet Team

unread,
Oct 15, 2012, 2:00:04 PM10/15/12
to mongod...@googlegroups.com
Hi,

I try to list all collections of a database using a client driver. What is equivalent driver for a "show collections" command in the console ?

----
The JMCNet Team.

Home page, downloads, wiki, support : http://code.google.com/p/jmcnet-full-mongo-flex-driver/

Forum : https://groups.google.com/forum/#!forum/jmcnet-full-mongo-flex-driver

Andre de Frere

unread,
Oct 15, 2012, 9:09:11 PM10/15/12
to mongod...@googlegroups.com
Hi,

Each of the drivers will do this in a slightly different way, but the concept remains the same.

For example, in the java driver, you can do "db.getCollectionNames();" as discussed at http://www.mongodb.org/display/DOCS/Java+Tutorial#JavaTutorial-GettingAListOfCollections

In the .Net driver you could use the "GetCollectionNames()" method from the "MongoDatabase" class, as shown in the API here: http://api.mongodb.org/csharp/current/html/aa54042b-8014-a9d8-06f0-4b090a634cc9.htm

Similarly, the PHP driver has a "getCollectionNames" here: http://www.php.net/manual/en/mongodb.getcollectionnames.php

Inside the Ruby driver, you can get a list of collection names with "db.collection_names" as discussed in the tutorial here: http://api.mongodb.org/ruby/current/file.TUTORIAL.html#Getting_a_List_Of_Collections

As mentioned above, the other drivers should have similar functionality in a way that makes sense for that language.

Hope this helps,
André

The JMCNet Team

unread,
Oct 16, 2012, 1:54:25 AM10/16/12
to mongod...@googlegroups.com
thank's for your answer, but my question was a little bit tricky. I'm writing a driver (for flash as3) and i wonder how to send a command on mongodb to get in response all collections of a database ?
it couldbe something like : db.$cmd.listCollections or something equivalent.

TheJmcnetTeam.

The JMCNet Team

unread,
Oct 16, 2012, 5:17:38 PM10/16/12
to mongod...@googlegroups.com
Hi,

It seems that we can do a query on system.namespaces collection.

But the answer looks like :

{ name:db_name.system.indexes}

{ name:db_name.fs.files}

{ name:db_name.fs.files.$_id_}

{ name:db_name.fs.files.$filename_1_uploadDate_1}

{ name:db_name.fs.chunks}

{ name:db_name.fs.chunks.$_id_}

{ name:db_name.fs.chunks.$files_id_1_n_1}

{ name:db_name.SomeCollection}

{ name:db_name.SomeCollection.$_id_}


So we have to filter the response. Is there a better way to get all collections of a database ?

Thank's for answer.

William Zola

unread,
Oct 16, 2012, 9:11:41 PM10/16/12
to mongod...@googlegroups.com
Look at the 'listDatabases' command:


This is the command that all of the drivers end up calling internally.

 -William 

The JMCNet Team

unread,
Oct 17, 2012, 7:01:05 AM10/17/12
to mongod...@googlegroups.com
OK. Thank's. It is exactly what I need.



The JMCNet Team

unread,
Oct 18, 2012, 5:08:20 PM10/18/12
to mongod...@googlegroups.com
Sorry but the listDatabases lists the "databases" and not the collections. Did I miss something ? Example of listDatabases return :

> use admin
switched to db admin
> db.runCommand({"listDatabases":1})
{
 
"databases" : [
 
{
 
"name" : "test_db",
 
"sizeOnDisk" : 83886080,
 
"empty" : false
 
},
 
{
 
"name" : "local",
 
"sizeOnDisk" : 1,
 
"empty" : true
 
}
 
],
 
"totalSize" : 83886080,
 
"ok" : 1
}

Any other idea ? How does the others drivers do to getCollectionNames ?

Thank's in advance.

Scott Hernandez

unread,
Oct 18, 2012, 5:12:17 PM10/18/12
to mongod...@googlegroups.com
As with most commands/methods in the mongo javascript shell looking at
the source helps (you can do this by removing the parens):

db.getCollectionNames
function () {
var all = [];
var nsLength = this._name.length + 1;
var c = this.getCollection("system.namespaces").find();
while (c.hasNext()) {
var name = c.next().name;
if (name.indexOf("$") >= 0 && name.indexOf(".oplog.$") < 0) {
continue;
}
all.push(name.substring(nsLength));
}
return all.sort();
}


So you want to look at the system.namespaces collection and filter out
the special ones.
> --
> You received this message because you are subscribed to the Google
> Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com
> See also the IRC channel -- freenode.net#mongodb
Reply all
Reply to author
Forward
0 new messages