To list the collections in a database, you need to start with a Db
object that points to the database you want to use. Then you call the collectionNames()
method on the Db
object. For example:
var newDB = db.db("newDB");
newDB.collectionNames(function(err, collectionNames)){}
The collectionNames()
method returns an array of objects that contains the collection names of the collections. For example:
[ { name: 'newDB.system.indexes' },
{ name: 'newDB.newCollection',
options: { create: 'newCollection' } } ]
You can also get back the collections of a database as an array of full Collection
objects by using the collections()
method. The collections()
method works exactly the same as the collectionNames()
method except that a fully usable Collection
object is created for each collection. For example:
var newDB = db.db("newDB");
newDB.collections(function(err, collectionList){}
The resulting value of the collectionList
parameter is an array of Collection
objects.