Currently, I am utilizing the native driver for mongoDB in my project. Within the database, there are approximately 7 collections that I need to work with. My goal is to create a variable that holds the count of entries in each collection except for the last one. Then, I want to create another variable specifically for the entries in the last collection. Finally, I intend to pass these variables through the res.render() command to display them on the webpage.
The main issue I'm facing here is that I am accustomed to synchronous execution of functions, which unfortunately does not apply in this scenario.
Below is the code snippet representing how I would approach this if everything were executed synchronously:
var count = 0;
db.listCollections().toArray(function(err,collection){
for(i = 1; i < collection.length;i++){
db.collection(collection[i].name).count(function(err,value){
count = count + value;
})
}
var count2 = db.collection(collection[i].name).count(function(err,value){
return value;
})
res.render('index.html',{data1: count, data2: count2})
})
Clearly, the above code does not achieve what I intended. I attempted to work with promises to solve the issue but only ended up more confused.