I need to write a function that return the number of documents in a collection using Node.js. This is my code (I've followed the advice found in another question):
async function countCollections(callback) {
MongoClient.connect(url, function(err, db) {
if (err) callback(error);
var dbo = db.db(nameDB);
dbo.collection(nameCollections).find(query).count(function(err, count) {
if (err) callback(error);
db.close();
callback(null, count);
});
});
}
I call this function in this way:
await countCollections(function(err, count) {
if (err) {
return console.log(err.message);
}
console.log('Number of documents: ', count);
});
Using this method, I can just view the count value, but I can't directly use. How can I do to obtain into a variable the count value when I call the function?