【发布时间】:2019-10-19 08:27:01
【问题描述】:
我有一个 mongodb,其中包含多个集合中的多个文档,我想遍历它的每个人。下面是我的代码,
const mongo = require('mongodb');
const url = 'mongodb://localhost:27017/test'
mongo.connect(url,{ useNewUrlParser: true }, data, (err, db)=>{
console.log('connection success');
db.db().listCollections().toArray(function(err, colls) {
colls.forEach(element => {
db.db().collection(element.name.toString()).find().toArray((err, doc) => {
console.log(doc);
});
});
});
db.close();
})
}
这是我从 listCollections() 得到的结果
[ ....,
{ name: 'documetn1',
type: 'collection',
options: {},
info: { readOnly: false, uuid: [Binary] },
idIndex:
{ v: 2, key: [Object], name: '_id_', ns: 'test.documetn1' } },
...]
不知怎的,我在下面得到了空值
db.db().collection(element.name.toString()).find().toArray((err, doc) => {
console.log(doc);
});
在这里需要帮助!
【问题讨论】:
-
打印
element.name.toString()并检查输出是什么。 -
也许更重要:打印
err