I want to connect to a mongodb database at cloud.mongodb.com using node.
I get the error mongodb.Connect is not a function. What am I doing wrong here?
const dotenv = require("dotenv");
dotenv.config();
const mongodb = require("mongodb");
const connectString = "my connection string";
mongodb.Connect(process.env.CONNECTIONSTRING, async function (err, client) {
const db = client.db();
const results = await db.collection("pets").find().toArray();
console.log(results);
});
JavaScript is case-sensitive. The function is connect, with a lower-case "c":
mongodb.connect(process.env.CONNECTIONSTRING, async function (err, client) {
// ---^
const db = client.db();
const results = await db.collection("pets").find().toArray();
console.log(results);
});
I believe this is due to one or both of these reasons:
MongoClient and not the entire mongodb moduleconst { MongoClient } = require("mongodb");
connect and not Connect