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!
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!
Before we dive into CRUD operations, let's set up our MongoDB environment.
Install MongoDB: Follow the official installation guide for your specific operating system.
Start the MongoDB service: Open a terminal and run mongod (on Windows, use mongod.exe).
Confirm the installation: Open another terminal and run mongo to connect to the MongoDB shell.
To create a new document, we use the db.collection.insertOne() method. Let's create a users collection and insert a user document.
// 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 });To read data, we use the db.collection.find() method. Let's retrieve all users from our users collection.
// Find all users
> db.users.find().toArray();To update data, we use the db.collection.updateOne() method. Let's update John Doe's age to 26.
// Update John Doe's age
> db.users.updateOne({ name: "John Doe" }, { $set: { age: 26 } });To delete data, we use the db.collection.deleteOne() method. Let's remove John Doe from our users collection.
// Delete John Doe
> db.users.deleteOne({ name: "John Doe" });What is the purpose of the `db.collection.insertOne()` method?
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.
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!