In the previous chapters, you learned the MongoDB basics. Although you worked with MongoDB code, you haven't yet connected that code to a server. In this chapter, you will build an Express server and connect it to MongoDB. You also will write routes and work with the data from MongoDB inside your Express app.
This chapter is different than previous ones. Previous chapters were instructional and demonstrative, whereas this chapter is more of a resources page. It provides a rough roadmap for building an Express app that connects to MongoDB. The end of this chapter contains tips, thoughts, and resources to help you get started with MongoDB, including a sample script for a basic Express-MongoDB application.
- What imports do you need for Express?
- How do you create an instance of the Express class?
- Do you know now of other imports you will need to start your Express server?
- How do you get your Express server to run and stay active?
- What are the steps for connecting your Express app to MongoDB Atlas?
- Do you need to install or import any software?
- Do you have a MongoDB connection string?
- What are you naming your database?
- What are you naming your collection?
- How can you confirm that the connection is successful?
- Do you need to?
- What are the pros and cons?
- Write the boilerplate for the route
- Which http method should you use?
- For whichever http method you use, how many arguments does that Express method accept?
- What are the data types those arguments?
- How will you handle errors inside your route function?
- Write your MongoDB request
- Which MongoDB method should you use to get all users?
- How do you write the line of code to use that method?
- Upon receiving a response from MongoDB, do you need to process the response before serving it to the client?
- How do you serve the response to the client?
- Do you need to install any extra packages?
- Write the boilerplate for the route
- Which http method should you use?
- For whichever http method you use, how many arguments does that Express method accept?
- What are the data types of those arguments?
- How will you handle errors inside your route function?
- When you send the request from the client, what information should you send to tell the server which user you want?
- How will you send the information from the client to the server?
- Inside your route, how will you access the information that identifies the user requested by the client?
- Write your MongoDB request
- Which MongoDB method should you use to get a single user?
- How do you write the line of code to use that method?
- What data type do you pass into the MongoDB method as an argument?
- Upon receiving a response from MongoDB, do you need to process the response before serving it to the client?
- How do you serve the response to the client?
- Do you need to install any extra packages?
- Write the boilerplate for the route
- Which http method should you use?
- For whichever http method you use, how many arguments does that express method accept?
- What are the data types of those arguments?
- How will you handle errors inside your route function?
- What information do you need to send from the client to the server?
- How will you send that information?
- Inside your route, how will you get the information sent from the client?
- Write your MongoDB request
- Which MongoDB method should you use to add a new user?
- How do you write the line of code to use that method?
- What data type do you pass into the MongoDB method as an argument?
- How will you know if the user was successfully added to the MongoDB database?
- How will you let the client know whether the user was successfully added?
- Do you need to install any extra packages?
- Upon receiving a response from MongoDB in your Express application for a get request, what information might you want to use to request more information from MongoDB (before even responding to the client)?
- Upon receiving a response from MongoDB in your Express application for a post request, what information might you want to use to request more information from MongoDB (before even responding to the client)?
- When storing information in a database, how will you organize your information?
- If in addition to a user collection, you also have a blog collection containing blog posts by each user, how will you know which user is the author for each blog post?
- How much search filtering should you do using MongoDB tools versus in your Express server script?
- Does MongoDB have a way to sort documents in a response?
- Does MongoDB have a way to count documents in a response?
- Does MongoDB have a way to limit documents in a response?
- Does MongoDB have a way to sort and limit?
- Does MongoDB have a way to search text in documents?
- Does MongoDB have a way to check whether a document has a certain field?
-
Take the time to learn how to connect your MongoDB cluster to your application. You need to know how to handle MongoDB account permissions, Cluster user permissions, connection strings, and IP addresses. It's not hard, but not knowing the ins and outs can lead to hours of looking in the wrong places to get your application working properly with MongoDB. When running your application, if you see an error like
MongoNetworkError: failed to connect to server [cluster.yours] on first connect [MongoNetworkError: connection 5 to cluster-yours closed
, then something is wrong with your permissions, connection string, or IP address. -
Take the time to properly learn how to hide your private MongoDB access information during development. It's tempting to cut corners when setting up your MongoDB connection by not hiding your private MongoDB information, especially when you're eager to write code and engineer your project. However, it's really important that you know how to secure your information. To hide your private information, use environment variables. For development purposes, store the environment variables in a file, add that file name to the
gitignore
file, and pull the information from yourgitignore
file into your app. This prevents your private file from saving on GitHub, yet allows you to use the private information in your application during development. Research the "node" way for doing this. -
If using environment variables, take the time to properly learn how to use environment variables in production. If for production you deploy your application from your GitHub repository, the environment variables will not be there. So, when you go to production, remember to add your environment variables to the production environment. The way to set environment variables for production depends upon how you deploy your application. Heroku, for instance, has clear instructions online and makes it easy to add environment variables.
-
MongoDB can be used with many different programming languages. When searching online to learn how to do something, be sure to include "Node" and "MongoDB" in your searches to help target answers. This is particularly helpful for getting the proper syntax. It can be helpful to review posts online that relate to MongoDB with languages other than NodeJS to help find the right approach. But beware that the syntax may vary some between that language and Node.
-
Sometimes your application will encounter an error because of a mismatch between what your code expects from the database and what your database has in it. When you get this kind of error, fix your code so that it can gracefully handle the situation without crashing. Also, clean up your database so that it matches what the code expects from it.
-
Practice reading error messages. Don't skim them. Read them carefully and think about what they mean. When you have an error, use the relevant part of the error message to search online.
-
MongoDB has great documentation on the basics. Use it. Their documentation can sometimes be confusing when it comes to more advanced usage. So beware.
-
Let's find some good StackOverflow Answers
- Cannot find module mongodb
- User not allowed to do action
- Avoid "current URL string parser is deprecated" warning
- UnhandledPromiseRejectionWarning
- Can't connect mongoDB atlas to heroku
- Find then is not a function
- Sort failure
- Connection timed out
- How can I use a cursor.forEach() in MongoDB using Node.js?
- db.collection is not a function
- MongoDB Econnrefused
- Async / await in express with multiple mongodb queries
- Duplicate key error index in mongodb mongoose
- How to sort a collection by date in MongoDB?
- Combine two OR-queries with AND in Mongoose
- Find documents with array that doesn't contains a specific value
- MongoDB Getting Started
- MongoDB Fundamentals
- Collection Methods
- Operators Homepage MongoDB
- StackOverflow MongoDB
- MongoDB Node Driver (NPM)
- MongoDB University - Free Learning
- MongoDB Instructor-Led Training
- Tutorial: Create and Query an Atlas Search Index
- Tutorials Point - MongoDB
- W3 Schools Node-MongoDB
- How to return only specific fields from a MongoDB query
Here is an example of an Express App connected to MongoDB. See if you can get it running on your computer. You'll need to consider how to connect it to your MongoDB account, whether you need to install any packages, what missing files and folders you need to add to your project folder, and anything else to get it working.
// Express setup
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const port = 3000;
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
let path = require("path");
app.use(express.static(path.join(__dirname)));
app.use("/styles", express.static(__dirname));
//Mongo setup
const { MongoClient, ObjectID } = require("mongodb");
const url =
"your connection string";
const client = new MongoClient(url, { useUnifiedTopology: true });
const dbName = "your database name";
let users_collection = "";
client.connect().then((response) => {
if (response.topology.s.state) {
console.log("Status: " + response.topology.s.state);
const db = client.db(dbName);
// Use the collection named "users"
users_collection = db.collection("users");
} else {
console.log("Problem connecting to MongoDB");
}
});
// Express Routes
app.get("/all-users", (req, res) => {
try {
// Get all users from Mongo
all_db_users = users_collection
.find()
.toArray()
.then((users) => {
res.send(users);
});
} catch (err) {
res.send(
`We have error: ${err.stack}. Sorry. We appreciate your patience while we work this out.`
);
}
});
app.get("/user/:id", (req, res) => {
try {
user_id = req.params.id;
// Get single user from Mongo
one_db_user = users_collection
.findOne({
_id: ObjectID(user_id),
})
.then((user_db) => {
res.send(user_db);
});
} catch (err) {
res.send(
`We have error: ${err.stack}. Sorry. We appreciate your patience while we work this out.`
);
}
});
app.get("/add-user-form", (req, res) => {
res.sendFile(path.join(__dirname + "/views/addUserForm.html"));
});
app.post("/add-user", (req, res) => {
try {
newUserDB = users_collection.insertOne(req.body);
newUserDB.then((response) => {
res.send(response.insertedId);
});
} catch (err) {
res.send(
`We have error: ${err.stack}. Sorry. We appreciate your patience while we work this out.`
);
}
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});