Comments (9)
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.
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.
db.getCollectionNames() has been added in 0.7.12 (see #73)
from mongojs.
Thanks all.
from mongojs.
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.
@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.
@sorribas Thanks, that worked!
In the README, the callback then should not be optional: db.getCollectionNames(callback)
instead of db.getCollectionNames([callback])
.
from mongojs.
@mb21 Good point. Will fix.
from mongojs.
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)
- Use native Object.assign instead of xtend HOT 1
- update `var` to `const` - Readme
- How to use collation HOT 2
- updateMany returns an error HOT 1
- Comparison with the official mongodb package
- Is $text supported within find()? HOT 1
- SCRAM-SHA-256 (and more)
- Publish version with mongodb ^3 HOT 3
- how to set remote connection to mongodb HOT 3
- How do i show dbs, similar to shell "show dbs" HOT 1
- Using replicaSet and readPreference option HOT 1
- "failed to connect to server" even after mongodb is back up HOT 1
- Compatible with hapijs 18? HOT 2
- MongoDB Atlas's URI does not work HOT 5
- collection.update no longer usable for overwriting an entry HOT 16
- v2.6.0 up to v3.1.0 update operation error! HOT 5
- .findOne() query just returns undefined on callback HOT 2
- Does this support aws document db?
- Found Vulnerability 'Regular Expression Denial of Service (ReDoS)' in Synk.io
- How to connect to mongo atlas?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongojs.