Getting Started with MongoDB for CRUD Operations

MongoDB is a popular NoSQL database known for its flexibility and scalability. It's a great choice for handling unstructured data and performing CRUD (Create, Read, Update, Delete) operations. In this article, we'll dive into the basics of MongoDB using the MongoDB shell and explore common operations.

Prerequisites

Before you get started, make sure you have MongoDB installed and running on your system. If you haven't already, you can download MongoDB from the official website.

Essential MongoDB Shell Commands

To work with MongoDB effectively, you need to know how to navigate the MongoDB shell. Here are some essential commands to get you started:

Listing Databases and Collections

  • To list all available databases:
show dbs
  • To list collections in the current database:
show collections

Viewing Documents

  • To find and view all documents in a collection in a specific database:
use your_database_name
db.collection_name.find()

Connecting to MongoDB

To start using MongoDB, open your terminal and connect to the MongoDB server using the mongo command:

mongo

This opens the MongoDB shell, and you are ready to work with your database.

Creating (Inserting) Documents

MongoDB stores data in collections, similar to tables in relational databases. You can insert documents into a collection using the insertOne() or insertMany() method.

Let's create a new collection called users and insert a document into it:

db.users.insertOne({
  name: "John Doe",
  age: 30,
  email: "johndoe@example.com"
})

Response:

{
  "acknowledged" : true,
  "insertedId" : ObjectId("5f6d51f327ba6a2d4c7a1bf2")
}

In the response, "acknowledged": true means that the insert operation was successful and acknowledged by the server. The "insertedId" field shows the unique identifier (_id) assigned to the newly inserted document.

Reading (Querying) Documents

You can retrieve data from MongoDB using the find() method. To find all documents in the users collection:

db.users.find()

Response:

{ "_id" : ObjectId("5f6d51f327ba6a2d4c7a1bf2"), "name" : "John Doe", "age" : 30, "email" : "johndoe@example.com" }

To filter documents based on specific criteria, such as finding users aged 30:

db.users.find({ age: 30 })
  • To retrieve a single document that matches specific criteria, such as finding users aged 30
db.users.findOne({ age: 30 })

Response:

{ "_id" : ObjectId("5f6d51f327ba6a2d4c7a1bf2"), "name" : "John Doe", "age" : 30, "email" : "johndoe@example.com" }

The find() method retrieves multiple documents, while findOne() retrieves a single document that matches the specified criteria. Both operations are powerful tools for querying data in MongoDB.

Updating Documents

To update documents, use the updateOne() or updateMany() method. For example, let's update John Doe's age:

db.users.updateOne(
  { name: "John Doe" },
  { $set: { age: 31 } }
)

Response:

{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

Deleting Documents

To delete documents, you can use the deleteOne() or deleteMany() method. To delete John Doe's document:

db.users.deleteOne({ name: "John Doe" })

Response:

{ "acknowledged" : true, "deletedCount" : 1 }

In the response, "acknowledged" : true confirms that the delete operation was acknowledged by the server. "deletedCount" shows how many documents were deleted.

Conclusion

You now have the essential commands to work with MongoDB shell for CRUD operations. Whether you're retrieving data, inserting, updating, or deleting documents, you have the basics covered. Stay tuned for upcoming articles where we'll explore advanced features like aggregation, indexing, and validation to take your MongoDB skills to the next level.