Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how do I do "show collections" in mongojs code? #54

Closed
xiaochunmei opened this issue May 13, 2013 · 9 comments
Closed

how do I do "show collections" in mongojs code? #54

xiaochunmei opened this issue May 13, 2013 · 9 comments

Comments

@xiaochunmei
Copy link

Mongo db shell support "show collections" to list all the collections of a database.
How do I do this in javascript using mongojs?

@sorribas
Copy link
Collaborator

sorribas commented Jul 3, 2013

While show collections itself is a mongo shell command and is not really JavaScript, you can get the list of collections with mongojs with something like the following:

var mongojs = require('mongojs');
var db = mongojs('test', []);

db.collections(function(e, cols) {
    cols.forEach(function(col) {
        console.log(col.collectionName);
    });
});

I hope this helps :)

@repoman
Copy link

repoman commented Aug 9, 2013

There is something called getCollectionNames() in the docs. Would be nice to see it in mongojs as well, not a big deal though.

@mafintosh
Copy link
Collaborator

db.getCollectionNames() has been added in 0.7.12 (see #73)

@xiaochunmei
Copy link
Author

Thanks all.

@mb21
Copy link

mb21 commented Aug 11, 2014

Am I missing something or is this currently (mongojs 0.13.1) broken?

> var mongojs = require("mongojs");
> var db = mongojs.connect("apis", ["apis"]);
> db.getCollectionNames
[Function]
> db.getCollectionNames()
undefined
> 
~/code/node_modules/mongojs/node_modules/mongodb/lib/mongodb/connection/base.js:245
        throw message;      
              ^
TypeError: undefined is not a function
    at ~/code/node_modules/mongojs/index.js:408:4
    at ~/code/node_modules/mongojs/node_modules/mongodb/lib/mongodb/db.js:524:5
    at ~/code/node_modules/mongojs/node_modules/mongodb/lib/mongodb/db.js:445:7
    at ~/code/node_modules/mongojs/node_modules/mongodb/lib/mongodb/cursor.js:163:16
    at commandHandler (~/code/node_modules/mongojs/node_modules/mongodb/lib/mongodb/cursor.js:706:16)
    at ~/code/node_modules/mongojs/node_modules/mongodb/lib/mongodb/db.js:1843:9
    at Server.Base._callHandler (~/code/node_modules/mongojs/node_modules/mongodb/lib/mongodb/connection/base.js:445:41)
    at ~/code/node_modules/mongojs/node_modules/mongodb/lib/mongodb/connection/server.js:468:18

@sorribas
Copy link
Collaborator

@mb21 You have to pass a callback to the getCollectionNames function. So you should do something like this.

db.getCollectionNames(function(err, colNames) {
  if (err) return  console.log(err);
  colNames.forEach(function(name) {
    console.log(name);
  });
});

@mb21
Copy link

mb21 commented Aug 14, 2014

@sorribas Thanks, that worked!

In the README, the callback then should not be optional: db.getCollectionNames(callback) instead of db.getCollectionNames([callback]).

@sorribas
Copy link
Collaborator

@mb21 Good point. Will fix.

@AndrewKralovec
Copy link

db.getCollectionNames(function(err, colNames) {
if (err) return console.log(err);
colNames.forEach(function(name) {
console.log(name);
});
});

This will just list the collections, which is already using inside the mongojs call // var db = mongojs(connectionString, [collections]
So pointless.
You will have to use mongodb and then use the admin option

// Use the admin database for the operation
var adminDb = db.admin();

// List all the available databases
adminDb.listDatabases(function(err, dbs) {
assert.equal(null, err);
assert.ok(dbs.databases.length > 0);
console.log(dbs);
db.close();
});
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants