MongoDB CRUD Operations Tutorial 🚀

beginner
21 min

MongoDB CRUD Operations Tutorial 🚀

Welcome to our comprehensive guide on MongoDB CRUD Operations! In this lesson, we'll be diving into Create, Read, Update, and Delete (CRUD) operations using MongoDB, a popular NoSQL database. By the end of this tutorial, you'll be equipped to manage data effectively and confidently! 💡 Pro Tip: MongoDB is widely used for its flexibility, scalability, and ease of use, making it an ideal choice for real-world projects!

What is MongoDB? 📝

MongoDB is a NoSQL database, meaning it uses a data model that is designed for handling structured and unstructured data in a flexible and scalable manner. Unlike traditional SQL databases, MongoDB stores data in JSON-like documents, making it a great choice for modern web applications!

Setting Up MongoDB 🎯

Before we dive into CRUD operations, let's set up our MongoDB environment.

  1. Install MongoDB: Follow the official installation guide for your specific operating system.

  2. Start the MongoDB service: Open a terminal and run mongod (on Windows, use mongod.exe).

  3. Confirm the installation: Open another terminal and run mongo to connect to the MongoDB shell.

Creating Data (CRUD - Create) 💡

To create a new document, we use the db.collection.insertOne() method. Let's create a users collection and insert a user document.

javascript
// Connect to the database (assuming "mydatabase" as the database name) > use mydatabase; // Create a users collection > db.createCollection("users"); // Insert a user document > db.users.insertOne({ name: "John Doe", age: 25 });

Reading Data (CRUD - Read) 📝

To read data, we use the db.collection.find() method. Let's retrieve all users from our users collection.

javascript
// Find all users > db.users.find().toArray();

Updating Data (CRUD - Update) 🎯

To update data, we use the db.collection.updateOne() method. Let's update John Doe's age to 26.

javascript
// Update John Doe's age > db.users.updateOne({ name: "John Doe" }, { $set: { age: 26 } });

Deleting Data (CRUD - Delete) 💡

To delete data, we use the db.collection.deleteOne() method. Let's remove John Doe from our users collection.

javascript
// Delete John Doe > db.users.deleteOne({ name: "John Doe" });

Quiz Time 📝

Quick Quiz
Question 1 of 1

What is the purpose of the `db.collection.insertOne()` method?

Advanced Example - CRUD Operations with Node.js 🎯

In a real-world application, you'd likely be using a Node.js server to perform CRUD operations. Here's a simple example using the mongodb package.

javascript
const { MongoClient } = require('mongodb'); const uri = 'mongodb://localhost:27017'; const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true }); client.connect(async (err) => { const collection = client.db("mydatabase").collection("users"); // Create a new user const result = await collection.insertOne({ name: "Jane Doe", age: 22 }); console.log("Inserted user: ", result.ops[0]); // Find all users const users = await collection.find().toArray(); console.log("Users: ", users); // Update Jane Doe's age const updateResult = await collection.updateOne({ name: "Jane Doe" }, { $set: { age: 23 } }); console.log("Updated user: ", updateResult.result); // Delete Jane Doe const deleteResult = await collection.deleteOne({ name: "Jane Doe" }); console.log("Deleted user: ", deleteResult.result); client.close(); });

And that's it for our MongoDB CRUD Operations tutorial! You're now well-equipped to handle CRUD operations effectively using MongoDB. 🎉 Happy coding!