MongoDB is a NoSQL database management system that can manage a humongous amount of data.
Unlike relational database management systems, MongoDB uses a NoSQL format to store and retrieve data.
NoSQL stands for Not Only Structured Query Language.
It means data is stored in various formats besides a traditional SQL table.
Rather than storing data in rows and columns within a table (as in SQL), MongoDB stores related data as a single document.
Think of each document as a single row in a SQL table:
Data in each document is stored as field-value pairs, similar to JSON format (technically BSON: Binary JavaScript Object Notation).
The general idea is that data frequently accessed together is stored together, rather than in separate tables, because SQL joins can be complex.
{
name: 'Spongebob',
age: 30,
gpa: 3.2,
fullTime: false
}
{
name: 'Patrick',
age: 38,
gpa: 1.5,
fullTime: false
}
{
name: 'Sandy',
age: 27,
gpa: 4,
fullTime: true
}This structure makes working with and scaling your database easy.
- Establish the connection by initiating the MongoDB shell using the
mongoshcommand. - Type
clsto clear the screen. - Type
exitto exit the MongoDB shell.
show dbs— Show all databases.use <database_name>— Use an existing database or create a new one.- Example:
use school
- Example:
A new database will not appear in the list until you add a collection to it.
Use the createCollection method:
db.createCollection("students")To drop the current database:
db.dropDatabase()Switch to the database you want to use:
use schooldb.students.insertOne({name: "Spongebob", age: 30, gpa: 3.2})db.students.insertMany([
{name: "Spongebob", age: 30, gpa: 3.2},
{name: "Patrick", age: 38, gpa: 1.5},
{name: "Sandy", age: 27, gpa: 4}
])- String: Series of text within quotes (e.g.,
"Monkey D Luffy") - Integer: Whole numbers (e.g.,
27) - Double: Numbers with decimal portions (e.g.,
3.2) - Boolean:
trueorfalse - Date Objects: Use
new Date()or pass a date string. - Null: Represents no value.
- Arrays: A field that can have more than one value.
- Nested Documents: Useful for addresses or embedded objects.
To sort documents, use method chaining with find() and sort():
db.students.find().sort({name: 1}) // Sort by name in ascending (A-Z) order
db.students.find().sort({name: -1}) // Sort by name in descending (Z-A) order
db.students.find().sort({gpa: 1}) // Sort by GPA ascending
db.students.find().sort({gpa: -1}) // Sort by GPA descendingUse the limit() method to restrict the number of documents returned:
db.students.find().limit(1)You can combine sort() and limit():
db.students.find().sort({gpa: -1}).limit(1) // Student with the highest GPAThe find() method returns all documents by default.
To filter results, use query and projection parameters:
db.students.find({query}, {projection})- Filters documents based on criteria.
Examples:
db.students.find({name: "Zoro"})
db.students.find({gpa: 2.5})
db.students.find({gpa: 3.8, fullTime: false})- Specifies which fields to return.
Examples:
db.students.find({}, {name: true}) // Only return names
db.students.find({}, {_id: false, name: true}) // Exclude _id, only names
db.students.find({}, {_id: false, name: true, gpa: true}) // Only names and GPAYou can update one or many documents.
- Takes two parameters: filter and update.
db.students.updateOne({filter}, {update})db.students.updateOne(
{name: "Sanji"},
{$set: {fullTime: true}}
)It's safer to update using the unique ObjectId, especially if names are not unique.
db.students.updateOne(
{_id: ObjectId("...")},
{$set: {fullTime: false}}
)Use the $unset operator to remove a field:
db.students.updateOne(
{name: "Sanji"},
{$unset: {fullTime: ""}}
)
















