MongoDB Quickstart
Introduction
Installation
$ brew tap mongodb/brew
$ brew install mongodb-community
- Installs mongodb server on your machine.1$ brew services start mongodb-community
- Starts mongodb server locally.2$ npm install mongodb
- Installs mongodb node package.
Usage
db/mongo-config.js:
const { MongoClient } = require("mongodb");
const URI = "mongodb://localhost:27017/";
const insertStuff = async () => {
const client = await new MongoClient(URI).connect();
const db = client.db("fec");
const collection = db.collection("items");
await collection.insertOne({ name: "stuff" });
// You have to close the connection, or the app will hang.
// You'll probably want to do this in an afterAll hook in your tests.
await client.close();
};
insertStuff();
References
- Bulk writes: https://docs.mongodb.com/manual/core/bulk-write-operations/
- API Docs: https://mongodb.github.io/node-mongodb-native/4.0/
- Shell Docs: https://docs.mongodb.com/mongodb-shell/connect/#std-label-mdb-shell-connect
2By default, this will run on localhost:27017
.