在完成本机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) 在open(/ 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) 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 在connectHandler(/ Users / sagarmunjal / Desktop / untitled folder / node_modules / mongodb / lib / server.js:286:7) 在g(events.js:273:16) 在emitOne(events.js:90:13) 在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) 在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]应用程序崩溃 - 在开始之前等待文件更改...
答案 0 :(得分:3)
mongoose.connection.db.collectionNames
已被弃用。我建议改用mongoose.connection.db.listCollections()
。
查看另一个stackoverlow帖子here
答案 1 :(得分:1)
这似乎有道理,Moongose确实没有收集游戏名称api。
尝试类似:
mongoose.connection.on('open', function(){
for (var i in mongoose.connection.collections) {
console.log(mongoose.connection.collections[i]);
}
});