Basic CRUD with Command Line

MongoDB is a popular NoSQL database known for its flexibility and scalability. Performing CRUD (Create, Read, Update, Delete) operations is fundamental when working with databases. This blog post will guide you through the basic CRUD operations using the MongoDB command line interface.

Prerequisites

  • MongoDB installed on your machine.

  • MongoDB service running.

  • Basic understanding of command line operations.

1. Setting Up MongoDB

Before we start with CRUD operations, ensure MongoDB is installed and running on your system. To start the MongoDB server, run:

mongod

Open another terminal window and connect to the MongoDB shell:

mongo

2. Creating a Database

MongoDB uses the concept of databases and collections. To create a database, simply switch to it using the use command. If the database does not exist, MongoDB will create it for you when you insert a document.

use myDatabase

Output:

switched to db myDatabase

3. Creating a Collection

A collection in MongoDB is equivalent to a table in relational databases. To create a collection, you can use the db.createCollection() method or insert a document directly, which will create the collection if it doesn’t exist.

db.createCollection("myCollection")

Output:

{ "ok" : 1 }

4. CRUD Operations

Create

To insert documents into a collection, use the insertOne() or insertMany() methods.

Insert One Document:

db.myCollection.insertOne({ name: "Alice", age: 25, city: "New York" })

Output:

{
    "acknowledged" : true,
    "insertedId" : ObjectId("...")
}

Insert Multiple Documents:

db.myCollection.insertMany([
    { name: "Bob", age: 30, city: "San Francisco" },
    { name: "Charlie", age: 35, city: "Los Angeles" }
])

Output:

{
    "acknowledged" : true,
    "insertedIds" : [
        ObjectId("..."),
        ObjectId("...")
    ]
}

Read

To retrieve documents from a collection, use the find() method.

Find All Documents:

db.myCollection.find()

Output:

{ "_id" : ObjectId("..."), "name" : "Alice", "age" : 25, "city" : "New York" }
{ "_id" : ObjectId("..."), "name" : "Bob", "age" : 30, "city" : "San Francisco" }
{ "_id" : ObjectId("..."), "name" : "Charlie", "age" : 35, "city" : "Los Angeles" }

Find with Query:

db.myCollection.find({ age: { $gt: 30 } })

Output:

{ "_id" : ObjectId("..."), "name" : "Charlie", "age" : 35, "city" : "Los Angeles" }

Update

To modify documents in a collection, use the updateOne() or updateMany() methods.

Update One Document:

db.myCollection.updateOne(
    { name: "Alice" },
    { $set: { age: 26 } }
)

Output:

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

Update Multiple Documents:

db.myCollection.updateMany(
    { city: "Los Angeles" },
    { $set: { city: "LA" } }
)

Output:

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

Delete

To remove documents from a collection, use the deleteOne() or deleteMany() methods.

Delete One Document:

db.myCollection.deleteOne({ name: "Bob" })

Output:

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

Delete Multiple Documents:

db.myCollection.deleteMany({ age: { $lt: 30 } })

Output:

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

5. Conclusion

Using the MongoDB command line interface, you can easily perform basic CRUD operations. This guide covered creating a database and collection, as well as how to insert, read, update, and delete documents. These fundamental operations form the backbone of working with MongoDB, enabling you to manage and manipulate your data efficiently.

For more advanced operations and configurations, refer to the MongoDB Documentation.

Last updated