MongoDB is a popular NoSQL database that provides high performance, high availability, and easy scalability. It's designed to handle large volumes of data across many servers, making it an excellent choice for modern web applications.
MongoDB is a document-oriented NoSQL database. Unlike traditional relational databases like MySQL or PostgreSQL, which use tables with rows and columns, MongoDB stores data in JSON-like documents. This makes it highly flexible and easy to work with complex data structures.
MongoDB can be installed on various operating systems. Here are the steps to install it on Ubuntu:
Import the public key used by the package management system:
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
Create a list file for MongoDB:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
Reload the package database:
sudo apt-get update
Install MongoDB packages:
sudo apt-get install -y mongodb-org
Start the MongoDB service:
sudo systemctl start mongod
Enable MongoDB to start on boot:
sudo systemctl enable mongod
You can connect to MongoDB using the mongo shell:
mongo
This will open the MongoDB shell where you can interact with your database.
A database in MongoDB is a container for collections. You can switch between databases using the use command:
use myDatabase
If the database does not exist, MongoDB will create it when you first store data in it.
Collections are similar to tables in relational databases. They store documents. Documents are JSON-like objects that can have different structures.
Documents are stored as BSON (Binary JSON) and can contain nested arrays and objects. Here is an example of a document:
{
"name": "John Doe",
"age": 30,
"address": {
"street": "123 Main St",
"city": "Anytown"
},
"phoneNumbers": [
{ "type": "home", "number": "555-1234" },
{ "type": "work", "number": "555-5678" }
]
}
To insert a document into a collection, use the insertOne or insertMany methods:
db.myCollection.insertOne({
name: "John Doe",
age: 30,
address: {
street: "123 Main St",
city: "Anytown"
}
})
To insert multiple documents:
db.myCollection.insertMany([
{ name: "Jane Doe", age: 25 },
{ name: "Jim Beam", age: 40 }
])
You can query documents using the find method. Here are some examples:
Find all documents:
db.myCollection.find()
Find documents with a specific field value:
db.myCollection.find({ name: "John Doe" })
Use projection to specify which fields to return:
db.myCollection.find({}, { name: 1, age: 1, _id: 0 })
To update documents, use the updateOne or updateMany methods:
Update a single document:
db.myCollection.updateOne(
{ name: "John Doe" },
{ $set: { age: 31 } }
)
Update multiple documents:
db.myCollection.updateMany(
{ age: { $lt: 30 } },
{ $inc: { age: 1 } }
)
To delete documents, use the deleteOne or deleteMany methods:
Delete a single document:
db.myCollection.deleteOne({ name: "John Doe" })
Delete multiple documents:
db.myCollection.deleteMany({ age: { $gt: 40 } })
To create an index on the name field:
db.myCollection.createIndex({ name: 1 })
MongoDB is a powerful NoSQL database that offers flexibility, scalability, and high performance. By understanding its basic concepts and operations, you can effectively use MongoDB to build modern web applications. This tutorial has covered the essentials of setting up MongoDB, performing CRUD operations, and implementing best practices.