Get ready for your next interview with our comprehensive question library
MongoDB is a document-oriented NoSQL database that stores data in flexible, JSON-like documents called BSON (Binary JSON). Key features include:
SQL Databases:
NoSQL Databases (MongoDB):
Choose MongoDB when:
BSON (Binary JSON) is the binary representation of JSON-like documents that MongoDB uses internally.
Key differences:
Example BSON types not in JSON:
{
_id: ObjectId("507f1f77bcf86cd799439011"),
created: ISODate("2023-01-01T12:00:00Z"),
data: BinData(0, "SGVsbG8gV29ybGQ=")
}
MongoDB organizes data in a three-level hierarchy:
Database: Top-level container for collections
Collection: Group of MongoDB documents
Document: Individual record in a collection
Example structure:
Database: ecommerce
├── Collection: users
│ ├── Document: {_id: 1, name: "John", email: "john@email.com"}
│ └── Document: {_id: 2, name: "Jane", age: 25, city: "NYC"}
└── Collection: products
└── Document: {_id: 1, title: "Laptop", price: 999.99}
ObjectId is a 12-byte unique identifier used as the default value for the _id
field.
Structure (24 hex characters):
Properties:
// ObjectId example
ObjectId("507f1f77bcf86cd799439011")
// Extract timestamp
ObjectId("507f1f77bcf86cd799439011").getTimestamp()
// Returns: ISODate("2012-10-17T20:46:47Z")
Create:
// Insert one document
db.users.insertOne({name: "John", age: 30})
// Insert multiple documents
db.users.insertMany([
{name: "Alice", age: 25},
{name: "Bob", age: 35}
])
Read:
// Find all documents
db.users.find()
// Find with criteria
db.users.find({age: {$gte: 25}})
// Find one document
db.users.findOne({name: "John"})
Update:
// Update one document
db.users.updateOne(
{name: "John"},
{$set: {age: 31}}
)
// Update multiple documents
db.users.updateMany(
{age: {$lt: 30}},
{$set: {status: "young"}}
)
Delete:
// Delete one document
db.users.deleteOne({name: "John"})
// Delete multiple documents
db.users.deleteMany({age: {$lt: 18}})
find():
findOne():
// find() - returns cursor
const cursor = db.users.find({age: {$gte: 25}})
cursor.forEach(doc => print(doc.name))
// findOne() - returns document
const user = db.users.findOne({email: "john@email.com"})
if (user) {
print(user.name)
}
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime