0% found this document useful (0 votes)
14 views

Mongo DB Notes

Mongo db notes

Uploaded by

reshminfathima2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Mongo DB Notes

Mongo db notes

Uploaded by

reshminfathima2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

MongoDB notes

Change or Create a Database :


use

eg:
use store

Show the Databases list


show dbs

Create Collection using mongosh :


Method 1
db.createCollection()

eg:
db.createCollection("products")

Method 2
db.collectionname.insertOne(object)

eg:
db.product.insertOne({icecream: "vanilla"})

Show the Collections


db.getCollectionNames()

Drop Collection
db.collectionname.drop()

MongoDB mongosh Insert:


Method 1
insert()

eg:
db.product.insertOne({
icecream: "kwality walls"
quantity: 3,
flavour: ["vanilla", "chocolate"],
date: Date()
})

Method 2
insertMany()

eg:
db.posts.insertMany([
{
chips: "lays",
type: "potato"
},
{
juice: "Mountain dew",
type: "cool drinks"
}
])

MongoDB mongosh Find:

Find Data
Method 1
find()

eg:
db.juniors.find()

Method 2
findOne()

eg:
db.posts.findOne()

Querying Data
eg:
db.produts.find( {icecream : "Kwality walls"} )

Projection
eg:
db.posts.find({}, {flavour: 1})

eg:
db.posts.find({}, {quality: 0})

MongoDB mongosh Update

Update Document
Method 1
updateOne()

eg:
db.produts.updateOne( { icecream: "kwality walls" }, { $set: { quantity: 20 } } )

Method 2
updateMany()

eg:
db.produts.updateOne( { icecream: "kwality walls" }, { $set: { quantity: 20 } } )

Insert if not found


eg:
db.posts.updateOne(
{ 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:
db.posts.deleteOne({ chip: "lays"})

Method 2
deleteMany()

eg:
db.posts.deleteMany({ 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
db.movies.aggregate([ { $limit: 1 } ])

Aggregation $project
eg:
$project
db.restaurants.aggregate([
{
$project: {
"name": 1,
"cuisine": 1,
"address": 1
}
},
{
$limit: 5
}
])

Aggregation $sort
eg:
$sort
db.listingsAndReviews.aggregate([
{
$sort: { "names": -1 }
}])

Aggregation $match
eg:
$match
db.listingsAndReviews.aggregate([
{ $match : { company: {$in:["PS8 Network"]} } }])

Aggregation $count
eg:
$count
db.restaurants.aggregate([
{
$match: { company: "PS8 Network" }
},
{
$count: "PS8 Network"
}
])

Aggregation $addFields
eg:
db.scores.aggregate( [
{
$addFields: {
totalHomework: { $sum: "$homework" }
}
}
])

You might also like