这个问题在这里已有答案:
在完成本机mongo命令之后,我正在尝试使用mongoose的ODM,它扩展了MongoDB的功能 .
我开始从我的localhost上的现有数据库中简单地获取我的集合 . 我能够连接到我的数据库,但我无法获得收藏 . 但是我无法这样做 .
/**
* Uses MongooseJS to Connect to MongoDB
* .Maps out all collections within
*/
var mongoose = require('mongoose')
, MONGO_DB = 'mongodb://localhost/astro';
mongoose.connect(MONGO_DB);
mongoose.connection.on('open', function(){
mongoose.connection.db.collectionNames(function(error, collections) {
if (error) {
throw new Error(error);
} else {
collections.map(function(collection) {
console.log('found collection %s', collection.name);
});
}
});
});
mongoose.connection.on('error', function(error){
throw new Error(error);
});
TypeError: mongoose.connection.db.collectionNames is not a function
at NativeConnection.<anonymous> (/Users/sagarmunjal/Desktop/untitled folder/script.js:29:26)
at emitNone (events.js:80:13)
at NativeConnection.emit (events.js:179:7)
at open (/Users/sagarmunjal/Desktop/untitled folder/node_modules/mongoose/lib/connection.js:511:11)
at NativeConnection.Connection.onOpen (/Users/sagarmunjal/Desktop/untitled folder/node_modules/mongoose/lib/connection.js:520:5)
at /Users/sagarmunjal/Desktop/untitled folder/node_modules/mongoose/lib/connection.js:476:11
at /Users/sagarmunjal/Desktop/untitled folder/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:60:5
at /Users/sagarmunjal/Desktop/untitled folder/node_modules/mongodb/lib/db.js:227:5
at connectHandler (/Users/sagarmunjal/Desktop/untitled folder/node_modules/mongodb/lib/server.js:286:7)
at g (events.js:273:16)
at emitOne (events.js:90:13)
at emit (events.js:182:7)
at /Users/sagarmunjal/Desktop/untitled folder/node_modules/mongodb-core/lib/topologies/server.js:504:23
at commandCallback (/Users/sagarmunjal/Desktop/untitled folder/node_modules/mongodb-core/lib/topologies/server.js:936:9)
at Callbacks.emit (/Users/sagarmunjal/Desktop/untitled folder/node_modules/mongodb-core/lib/topologies/server.js:116:3)
at null.messageHandler (/Users/sagarmunjal/Desktop/untitled folder/node_modules/mongodb-core/lib/topologies/server.js:282:23)
/ Users / sagarmunjal / Desktop / untitled folder / node_modules / mongodb / lib / server.js:289 process.nextTick(function(){throw err;})^
TypeError:mongoose.connection.db.collectionNames不是NativeConnection中的函数 . (/ Users / sagarmunjal / Desktop / untitled folder / script.js:29:26)在emitNone(events.js:80:13)的NativeConnection.emit(events.js:179:7)中打开(/ Users / sagarmunjal) / Desktop / untitled文件夹/ node_modules / mongoose / lib / connection.js:511:11)在NativeConnection.Connection.onOpen(/ Users / sagarmunjal / Desktop / untitled folder / node_modules / mongoose / lib / connection.js:520:5) )/ users / sagarmunjal / Desktop / untitled folder / node_modules / mongoose / lib / connection.js:476:11 at / Users / sagarmunjal / Desktop / untitled folder / node_modules / mongoose / lib / drivers / node-mongodb-native / connection.js:60:5 at / Users / sagarmunjal / Desktop / untitled folder / node_modules / mongodb / lib / db.js:227:5 at connectHandler(/ Users / sagarmunjal / Desktop / untitled folder / node_modules / mongodb / lib / server.js:286:7)at g(events.js:273:16)在emitOne(events.js:90:13)的emit(events.js:182:7)/ users / sagarmunjal / Desktop / untitled文件夹/ node_modules / mongodb -core / lib /拓扑/ server.js:504:23 at commandCallback(/ Users / sagarmunjal / Desktop / untitled文件夹/ node_modules / mongodb -core / lib / topologies / server.js:936:9)在Callbacks.emit(/ Users / sagarmunjal / Desktop / untitled folder / node_modules / mongodb-core / lib / topologies / server.js:116 :3)at null.messageHandler(/ Users / sagarmunjal / Desktop / untitled folder / node_modules / mongodb-core / lib / topologies / server.js:282:23)[nodemon] app崩溃 - 在开始之前等待文件更改.. .
2 回答
mongoose.connection.db.collectionNames
已被弃用 . 我建议改用mongoose.connection.db.listCollections()
.查看另一个stackoverlow帖子here
这似乎有道理,Moongose真的没有collectionNames api .
尝试类似的东西: