Setting-up a Database
Learn how to set up a MongoDB database for a Node.js project.
MongoDB in 60 seconds
We won’t use any advanced MongoDB features in this course, so not having any prior MongoDB knowledge is fine. But let’s define some common terminology we’ll use when discussing MongoDB.
In MongoDB, data is stored in collections, similar to tables in relational databases. A collection consists of individual JSON objects. Each object in MongoDB has an ID, which is defined by the _id
field. This field usually has a special format called ObjectId
.
MongoDB does not enforce any specific schema for these objects, so a single Mongo collection can have heterogeneous objects. However, in Mongoose (the library we will use to work with MongoDB data), we can specify which schema we want to enforce on our objects.
In our database, we will have three collections that correspond to the types we’ve already created: products, users, and categories.
A note on the lesson’s content
This lesson has been included just so that we can see all the steps involved with setting up a GraphQL application. If you’re already familiar with how to integrate MongoDB with GraphQL, you can jump to the next lesson.
Setting up MongoDB
To make it easier to get this application running, this lesson includes a Docker Compose file that starts two containers:
mongo
: A database server that will handle all read and write operations.mongo-express
: A web-based MongoDB client that interacts with our database.
Alternatively, we can use the desktop application Mongo Compass to inspect data in a Mongo database.
Here is the Docker Compose file that we will use:
Get hands-on with 1200+ tech skills courses.