MongoDB notes
Change or Create a Database :
use
eg:
use store
Show the Databases list
show dbs
Create Collection using mongosh :
Method 1
[Link]()
eg:
[Link]("products")
Method 2
[Link](object)
eg:
[Link]({icecream: "vanilla"})
Show the Collections
[Link]()
Drop Collection
[Link]()
MongoDB mongosh Insert:
Method 1
insert()
eg:
[Link]({
icecream: "kwality walls"
quantity: 3,
flavour: ["vanilla", "chocolate"],
date: Date()
})
Method 2
insertMany()
eg:
[Link]([
{
chips: "lays",
type: "potato"
},
{
juice: "Mountain dew",
type: "cool drinks"
}
])
MongoDB mongosh Find:
Find Data
Method 1
find()
eg:
[Link]()
Method 2
findOne()
eg:
[Link]()
Querying Data
eg:
[Link]( {icecream : "Kwality walls"} )
Projection
eg:
[Link]({}, {flavour: 1})
eg:
[Link]({}, {quality: 0})
MongoDB mongosh Update
Update Document
Method 1
updateOne()
eg:
[Link]( { icecream: "kwality walls" }, { $set: { quantity: 20 } } )
Method 2
updateMany()
eg:
[Link]( { icecream: "kwality walls" }, { $set: { quantity: 20 } } )
Insert if not found
eg:
[Link](
{ title: "Post Title 5" },
{
$set:
{
title: "Post Title 5",
body: "Body of post.",
category: "Event",
likes: 5,
tags: ["news", "events"],
date: Date()
}
},
{ upsert: true }
)
MongoDB mongosh Delete
Delete Documents
Method 1
deleteOne()
eg:
[Link]({ chip: "lays"})
Method 2
deleteMany()
eg:
[Link]({ college: "mce"})
MongoDB Query Operators
Comparison
$eq: Values are equal
$ne: Values are not equal
$gt: Value is greater than another value
$gte: Value is greater than or equal to another value
$lt: Value is less than another value
$lte: Value is less than or equal to another value
$in: Value is matched within an array
Logical
$and: Returns documents where both queries match
$or: Returns documents where either query matches
$nor: Returns documents where both queries fail to match
$not: Returns documents where the query does not match
Evaluation
$regex: Allows the use of regular expressions when evaluating field values
$text: Performs a text search
$where: Uses a JavaScript expression to match documents
MongoDB Update Operators
Fields
$currentDate: Sets the field value to the current date
$inc: Increments the field value
$rename: Renames the field
$set: Sets the value of a field
$unset: Removes the field from the document
Array
$addToSet: Adds distinct elements to an array
$pop: Removes the first or last element of an array
$pull: Removes all elements from an array that match the query
$push: Adds an element to an array
MongoDB Aggregation Pipelines
Aggregation limit
eg:
$limit
[Link]([ { $limit: 1 } ])
Aggregation $project
eg:
$project
[Link]([
{
$project: {
"name": 1,
"cuisine": 1,
"address": 1
}
},
{
$limit: 5
}
])
Aggregation $sort
eg:
$sort
[Link]([
{
$sort: { "names": -1 }
}])
Aggregation $match
eg:
$match
[Link]([
{ $match : { company: {$in:["PS8 Network"]} } }])
Aggregation $count
eg:
$count
[Link]([
{
$match: { company: "PS8 Network" }
},
{
$count: "PS8 Network"
}
])
Aggregation $addFields
eg:
[Link]( [
{
$addFields: {
totalHomework: { $sum: "$homework" }
}
}
])