Install MongoDB itself

First install MongoDB on your Mac
$ brew tap mongodb/brew
$ brew install mongodb-community@5.0 (had to update xcode before it worked)
add to PATH:
$ echo 'export PATH="/opt/homebrew/opt/mongodb-community@5.0/bin:$PATH"' >> ~/.zshrc
add a service so it starts and will be automatically restarted at login:
$ brew services start mongodb/brew/mongodb-community@5.0
If you want to stop the service later you can run
$ brew services stop mongodb-community@5.0

Install node Module

Now install the mongodb module
$ npm install mongodb
import { MongoClient } from "mongodb"; const client = new MongoClient("mongodb://localhots:27017"); const dbName = "myDB"; const main = async () => { await client.connect(); //initiate a connection const db = client.db(dbName); //connect to the database const collection = db.collection("myCollection"); //get one of the collections in the database return collection; }; const coll = await main();

Leave a comment