To fix TypeError: db.collection is not a function with Node MongoDB, we should make sure we’re getting the collection
from the database object.
For instance, we write
MongoClient.connect(db.url, (err, database) => {
const myAwesomeDB = database.db("myDatabaseNameAsAString");
myAwesomeDB.collection("theCollectionIwantToAccess");
});
to call connect
to connect to the database.
And we get the database from the database.db
method.
We then get the collection from the database’s collection
property.