我在通过节点在mongodb中列出集合时遇到问题。要设置mongo,我做了以下工作:
brew tap mongodb/brew
brew install mongodb-community@4.2
brew services start mongodb-community@4.2
然后,我像这样创建了一个数据库和集合:
mongo
use mongo-crud
db.createCollection('dogs')
我可以通过命令行看到数据库和集合已成功创建。现在我的节点应用程序是这样的:
const MongoClient = require('mongodb').MongoClient;
// Assert is not really needed though we can use it to check for errors
const assert = require('assert');
// Note that this is the default port that must be used
const url = 'mongodb://localhost:27017';
const dbName = 'mongo-crud';
// useUnifiedTopology is needed to hide a warning
const client = new MongoClient(url, { useUnifiedTopology: true });
// Use connect method to connect to the Server
client.connect(function(err) {
assert.equal(null, err);
console.log("Connected successfully to server");
const db = client.db(dbName);
db.listCollections().toArray(function(err, collInfos) {
console.log(err)
assert.equal(err, null);
});
client.close();
});
There doesn't seem to be an issue connection to the database but when I try to list the collections it throws an error name: 'MongoError' [Symbol(mongoErrorContextSymbol)]: {}
. What am I doing wrong here?