Giter Club home page Giter Club logo

Comments (9)

sorribas avatar sorribas commented on June 4, 2024 1

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 :)

from mongojs.

repoman avatar repoman commented on June 4, 2024

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

from mongojs.

mafintosh avatar mafintosh commented on June 4, 2024

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

from mongojs.

xiaochunmei avatar xiaochunmei commented on June 4, 2024

Thanks all.

from mongojs.

mb21 avatar mb21 commented on June 4, 2024

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

from mongojs.

sorribas avatar sorribas commented on June 4, 2024

@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);
  });
});

from mongojs.

mb21 avatar mb21 commented on June 4, 2024

@sorribas Thanks, that worked!

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

from mongojs.

sorribas avatar sorribas commented on June 4, 2024

@mb21 Good point. Will fix.

from mongojs.

AndrewKralovec avatar AndrewKralovec commented on June 4, 2024

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();
});
});

from mongojs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.