- MongoDB Database
- Setting up MongoDB
- What are schema and models?
- MongoDB is a no-SQL database.
- If you are from an SQL background, a collection represents a table of rows and columns.
- A document represents a row in a table.
- MongoDB has a cloud solution known as MongoDB Atlas platform. You can create a free shared cluster for your database.
- Follow instructions on how to register a new Atlas account here. You can use email, GitHub account, or google account.
- Then follow on how to deploy a free shared cluster here (Select Atlas UI tab for clear guidance).
- If you have not created a database user, follow the below steps.
- From the sidebar, under Security, select Database Access.
- Select +ADD NEW DATABASE USER
- Choose Authentication Method->Password.
- Fill in your username and password.
- Under Built-in Role, select Atlast Admin
- Then click the Add User button.
- You should make a list of trusted IP addresses to validate access to your cluster and data.
- You should whitelist your IP address to validate your connection with the cloud service.
- From the sidebar, under Security, select Network Access.
- Select +ADD IP ADDRESS
- Select the ADD CURRENT IP ADDRESS button and confirm.
- From the sidebar, under Deployment select Database and then your cluster name to see an overview page.
- In the cluster's overview page, select the Collections tab. Then select the +Create Database button.
- Then fill Database Name: my-blog and Collection Name: authors
- From the sidebar under Deployment, select Database and select your cluster.
- Click Connect button
- Select the Connect your application option
- From the dropdown menu, select DRIVER and VERSION.
- Copy the provided connection string and go back to our project folder.
Place the connection string in our
.envfolder like this:
<password>with the credential of the database user.
<clusterName>will be there already.
Add the database name after
mongodb.net/. Since we have created a database named
my-blogour config will look like this:
Navigate to the
src/config.jsfile and add the following line in our export:
srcand create a
connectDB.jsfile and place this code snippet:
The schema of a collection describes fields, field types, and other metadata for that collection. It is a blueprint for collection or any database entity.
From schema, we can create a constructor called Model. An instance of a model is called a document. Models are responsible for creating and reading documents from the underlying MongoDB database. Let's code to understand the concept.
We will have two collections under the database my-blog: authors and posts. For this first, we will create the schema for each collection and export the model for each schema.
Navigate to the
srcfolder and create two folders:
post. Each folder will contain all layers for each collection.
schema.jsfile inside both the
The project structure will look something like this:
From mongoose, we are using the
Schemaclass to define a schema for the author.
The author schema will have a name, biodata, and an array of social media links.
Then we will create a model using schema and export it.
Initiating the author model will provide the necessary functions to read and write the author's documents in MongoDB database.
We will repeat the same steps with post schema
MongoDB creates a unique id (ObjectId) for each document. We will use the author's unique id in the post schema as
authorRef. It will be helpful to trace out the author of a particular post.
- We have created a database my-blog with a collection author.
- We got our connection string and added it to our configuration.
- We have created schemas for the author and the post.
- In the next part, we will take a look at services.
- The link to the project repo is available here.