Lesson 24 of 30
MongoDB with Node.js
80% complete
Chapters
Chapter 24
MongoDB with Node.js
Learn the basics of connecting a Node.js application to MongoDB.
Theory
MongoDB is a NoSQL document database.
Node.js applications can communicate with MongoDB using official drivers or libraries such as Mongoose.
A typical application performs operations such as:
• Connecting to the database
• Creating documents
• Reading documents
• Updating documents
• Deleting documents
Mongoose provides schemas and models that make MongoDB application development easier.
Example
Node.js Example
const mongoose = require("mongoose");
mongoose
.connect("mongodb://127.0.0.1:27017/myapp")
.then(() => {
console.log("MongoDB connected");
})
.catch((error) => {
console.error(error);
});Output
The Node.js application attempts to connect to MongoDB.
Practice
Create a Node.js application that connects to MongoDB and stores user data.
Interview Questions
1.
What is MongoDB?
2.
How can Node.js connect to MongoDB?
3.
What is Mongoose?
Quick Quiz
Which option best matches this chapter?
A.A NoSQL document database
B.A CSS framework
C.A JavaScript compiler
D.An operating system
Answer: A NoSQL document database