Reputation: 3756
I'm trying to return a list of a dbs collections using mongoose. I'm following the directions set out here but http://grokbase.com/t/gg/mongoose-orm/122xxxr7qy/mongoose-get-a-list-of-all-collections. So here is my code
var mongoose = require('mongoose');
//if (mongoose.connection.readyState == 0){//checks if already connected to the database
console.log("creating connection to the database");
var Config = require('../configs/config');
var config = new Config();
config = config.getConfig().db.dev;
if (mongoose.connection.readyState = 0 ) {
mongoose.connect("mongodb://austin:[email protected]:10023/test1");
console.log('mongoose readyState is ' + mongoose.connection.readyState);
}
var collection;
mongoose.connection.on('open', function (ref) {
console.log('Connected to mongo server.');
});
//trying to get collection names
mongoose.connection.db.collectionNames(function (err, names) {
console.log(names); // [{ name: 'dbname.myCollection' }]
module.exports.Collection = names;
});
the only problem is that names returns as undefined. So is it even possible to return a list of collections using just vanilla mongoose?
Upvotes: 26
Views: 76783
Reputation: 869
import mongoose from "mongoose";
// code to create a connection..etc.
const db = mongoose.connection.db;
const collections = await db.listCollections().toArray();
console.log(collections);
This works for me. Furthermore, you can loop through the output to get the desired output format.
ps: According to MongoDb doc, we have certain fields like nameOnly
, and filter
to apply on the listCollections
query. But that did not work for me with Mongoose. Please add in the comments if you get something.
Thanks!
Upvotes: 0
Reputation: 21
I tried all the previous answers and didn't receive a positive result. But I have got the array of collections with this code:
let listOfCollections = Object.keys(mongoose.connection.collections);
Upvotes: 2
Reputation: 194
Similar to the top answer above, but this code displays the name for each collection, without the other added information (such as type, options, info, etc.).
const namesList = [];
mongoose.connection.on("open", function (ref) {
console.log("Connected to mongo server.");
//trying to get collection names
mongoose.connection.db.listCollections().toArray(function (err, names) {
for (let i = 0; i < names.length; i++) {
// gets only the name and adds it to a list
const nameOnly = names[i].name;
namesList.push(nameOnly);
}
console.log(namesList);
module.exports.Collection = namesList;
});
});
Upvotes: 0
Reputation: 3440
Try running your collection names function after connection.
mongoose.connection.on('open', function (ref) {
console.log('Connected to mongo server.');
//trying to get collection names
mongoose.connection.db.listCollections().toArray(function (err, names) {
console.log(names); // [{ name: 'dbname.myCollection' }]
module.exports.Collection = names;
});
})
Upvotes: 31
Reputation: 32767
If you are only working with Mongoose Models, that is all you need:
const connection = mongoose.connection;
Object.keys(connection.models).forEach((collection) => {
// You can get the string name.
console.info(collection);
// Or you can do something else with the model.
connection.models[collection].remove({});
});
Upvotes: 0
Reputation: 609
Here is how I managed to obtain all the names on the connected db.
var mongoose = require('mongoose');
var collections = mongoose.connections[0].collections;
var names = [];
Object.keys(collections).forEach(function(k) {
names.push(k);
});
console.log(names);
This solution works good on mongoose 4.4.19.
Upvotes: 7
Reputation: 927
Just came across this answer and though it may have worked at the time it appears collectionNames
has been removed from the available function names in favour of listCollections
This other stack overflow post has a working example: https://stackoverflow.com/a/29461274/4127352
Here is the link to the original docs: http://mongodb.github.io/node-mongodb-native/2.0/meta/changes-from-1.0/
Upvotes: 48