MongoDB is a popular, NoSQL (non-relational) database management system (DBMS). MongoDB uses JSON-like documents with optional schemas.
- Use MongoDB when you need a flexible and schema-less database.
- Choose MongoDB for applications with large amounts of unstructured or semi-structured data.
- MongoDB is suitable for real-time data processing and analytics.
- It works well for high-read workloads and scaling horizontally.
- A better fit for applications that require low-latency data.
- A better choice for distributed applications, where data needs to be spread across multiple servers.
-
Collection: A grouping of documents inside of a database. This is the same as a table in SQL and usually, each type of data (users, posts, products) will have its own collection.
-
Document: A record inside of a collection. This is the same as a row in SQL and usually, there will be one document per object in the collection. A document is also a JSON object.
-
Field: A key-value pair within a document. This is the same as a column in SQL. Each document will have some number of fields that contain information such as name, address, hobbies, etc. A significant difference between SQL and MongoDB is that a field can contain values such as JSON objects, and arrays instead of just strings, numbers, booleans, etc.
brew tap mongodb/brew
brew update
brew install [email protected]
# for shell
brew install mongosh
brew services start [email protected]
brew services stop [email protected]
Note: For other OS please follow these instructions.
mongosh
show dbs
db
use posts
db.dropDatabase()
db.createCollection('posts')
show collections
db.posts.insertOne({
title: 'Post One',
body: 'Body of post one',
tags: ['news', 'events'],
reactions: 2,
views: 20,
userId: 1,
})
db.posts.insertMany([
{
title: 'Post Two',
body: 'Body of post two',
category: 'Technology',
tags: ['news', 'events'],
reactions: 20,
views: 30,
userId: 1,
date: Date()
},
{
title: 'Post Three',
body: 'Body of post three',
category: 'News',
tags: ['news', 'events'],
reactions: 12,
views: 10,
userId: 1,
date: Date()
},
{
title: 'Post Four',
body: 'Body of post three',
category: 'Entertainment',
tags: ['news', 'events'],
reactions: 10,
views: 3,
userId: 1,
date: Date()
}
])
db.posts.find()
db.posts.find().pretty()
db.posts.find({ userId: 1 })
db.posts.findOne({ id: 1 })
db.posts.find({ title: 'Post One' }, {
title: 1,
userId: 1
})
db.posts.countDocuments()
# asc
db.posts.find().sort({ title: 1 }).pretty()
# desc
db.posts.find().sort({ title: -1 }).pretty()
db.posts.find().limit(2).pretty()
db.posts.find().limit(2).sort({ title: 1 }).pretty()
db.posts.find({ views: { $gt: 2 } })
db.posts.find({ views: { $gte: 7 } })
db.posts.find({ views: { $lt: 7 } })
db.posts.find({ views: { $lte: 7 } })
db.posts.find({ name: { $eq: "shahriar" } })
db.posts.find({ name: { $ne: "shahriar" } })
db.posts.find({ name: { $in: ["shahriar", "swim"] } })
db.posts.find({ name: { $nin: ["shahriar", "swim"] } })
db.posts.find({$or: [{name: "shahriar"}, {views: 3}]})
db.posts.find({name: {$not: {$eq: "shahriar"}}})
db.posts.find({name: {$exists: true}})
db.posts.find({ $expr: { $gt: ["$views", "$reactions"] } })
db.posts.updateOne({ title: 'Post Two' },
{
title: 'Post Two',
body: 'New body for post 2',
date: Date()
})
db.posts.updateOne({ title: 'Post Two' },
{
$set: {
body: 'Body for post 2',
}
})
db.posts.updateMany({ userId: 1 },
{
$set: {
userId: 2
}
})
db.posts.updateOne({ title: 'Post Two' },
{
$inc: {
reactions: 5
}
})
db.posts.updateOne({ title: 'Post Two' },
{
$rename: {
reactions: 'favorites'
}
})
db.posts.replaceOne({ title: 'Post Two' },
{
$unset: {title: ""}
})
db.posts.updateOne({ title: 'Post Two' },
{
$push: {tags: "new tag"}
})
db.posts.updateOne({ title: 'Post Two' },
{
$pull: {tags: "new tag"}
})
db.posts.deleteOne({ title: 'Post Four' })
db.posts.deleteMany({ userId: 1 })