i have a mongodb with multiple documents inside of multiple collections, and I want to loop through everyone of it. Below is my code,
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();
})
}
this is what I get back from listCollections()
[ ....,
{ name: 'documetn1',
type: 'collection',
options: {},
info: { readOnly: false, uuid: [Binary] },
idIndex:
{ v: 2, key: [Object], name: '_id_', ns: 'test.documetn1' } },
...]
somehow i am getting null for below
db.db().collection(element.name.toString()).find().toArray((err, doc) => {
console.log(doc);
});
need help here!