Before We Begin
So this is my summary for the playlist of introduction to a MongoDB, at this post we will interact only with the pure environment so you could implement it in any web framework.
Download
Note
Download the latest or just the latest version that is in the video (5.0.7)
2- Download MongoDB tool (MongoDB Shell)
document-store-100893897-large.webp
MongoDB contains databases(non-SQL), that databases contain something called collections, and collections it's like when you go to your folders on your pc, you call to see all the folders that are the collections in MongoDB
. then in every collection, we can see documents, and docs it's like the files in your folders. but the main advantage for the js developers is that documents are like JSON files so it's very familiar for web dev
{
"title":"my first post",
"auther":"hamdy saad",
"tages":["database","web dev"],
"reactions":32,
}
Note
1- it could be a nested document too
{
"title":"my first post",
"auther":{
"name":"Hamdy Saad"
"age":21
"country":"Egypt"
},
"tages":["database","web dev"],
"reactions":32,
}
2- the id will be created automatically so don't worry about that
Example with the id :
{
"title":"my first post",
"auther":"hamdy saad",
"tages":["database","web dev"],
"reactions":32,
"_id": Objectid("w12uwh2wq214")
}
Working with commands
Start working with MongoDB (start the shell)
mongosh
Note (if it's not working make sure that you downloaded it correctly)
Show the databases that are available
show dbs
Use one database
for example, we have 3 databases:
-admin 41 kB
-config 36.9 kB
-local 41 kB
and I want to use the admin
database.
use admin
and you can see that it's switched
Note (if you use a database that doesn't exist it will create it)
for example, we have 3 databases:
-admin 41 kB
-config 36.9 kB
-local 41 kB
and I want to create the testdb
database.
use testdb
and you can see that it created it
Clear screen
cls
See the current database that I'm in
db
See all the collections inside that database
show collections
Use help to see all the commands
help
Create a new document inside the database
db.[collectionName].insert({})
Ex:
use bookstore
db.books
db.books.insertOne({})
insertOne vs insertMany
insertOne
for one doc and insertMany
for Many (SIMPLE ๐!)
insertOne()
use bookstore
db.books
db.books.insertOne({
"title":"my first post",
"auther":"hamdy saad",
"tages":["database","web dev"],
"reactions":32,
})
insertMany()
it's an array of docs
use bookstore
db.books
db.books.insertMany([{
"title":"my first post",
"auther":"hamdy saad",
"tages":["database","web dev"],
"reactions":32,
},{
"title":"my first post",
"auther":"hamdy saad",
"tages":["database","web dev"],
"reactions":32,
}])
find a doc in the Collection
db.books.find({"auther":"hamdy saad"}) // out put first 20 .
// will return every doc that contains this auther name .
you can filter the docs even more by adding a second argument
db.books.find({"auther":"hamdy saad"},{"title": 1}).
so what we did is :
1- going throw the books collections
2- filter to get only docs that contain the author name Ex:" author": "hamdy saad"
3- get from the selected docs only the things that in the second argument Ex:" title": 1
sorting the docs & limit the data
count (the number of results)
db.books.find({"auther":"hamdy saad"}).count.
limit (limit the number of results)
db.books.find({"auther":"hamdy saad"}).limit(3).
sort (1 || -1)
db.books.find().sort({"title":1}).
// it's sorting the docs according to the title
// 1 is for ascending order and -1 for descending order
not exact values
- so in the past steps we filtered the collection in terms of the title and author name, but now we want to do something not specific,
for Example, I
need all the books that have more than 5 reactions .
$ (operater)
db.books.find({"reactions":{"$gt":5}})
- gt =
greater than
- lt =
less than
- lte =
less than or equal to
- gte =
greater than or equal to
- or =
takes more than one, so it's an
array - in =
array of values
- nin =
array of values (not in)
db.books.find($or[{"reactions":{"$gt":5}},{"reactions":{"$lt":5}}])
// get docs are 5 or bigger or less than 5 reactions
db.books.find($or[{"reactions":5},{"reactions":7}])
// get docs are 5 or 7 reactions.
$in Ex:
db.books.find({"reactions":{"$in":[5,7]}})
// get docs are 5 or 7 reactions
$nin Ex:
db.books.find({"reactions":{"$nin":[5,7]}})
// get docs are not 5 or 7 reactions
Tricky one here !!
- if you have on your collections 12 docs: 2-fantasy 4-comedy 6-romance and the doc could have one or more genres. If you want the fantasy only, we could use the find :
db.books.find({"genres":"fantacy"})
but using this command could result to show a doc that has more than one genre.
- if you need what you looking for specifically, you could put the values in an array like so :
db.books.find({"genres":["fantacy"]})
Now delete the doc
deleteOne()
deleteMany()
db.books.deleteOne({"title":"my first post"})
// delete the first doc that has the title "my first post"
db.books.deleteMany({"title":"my first post"})
// delete all the docs that has the title "my first post"
- `the best way to delete a document is by its id? cuz it's unique
Update the doc
UpdateOne() + $set
### UpdateMany() + $set
- By its unique `_id`
db.books.UpdateOne({"_id":"5e9f9b8f9c8f8c0f8c8f8f8f"},{$set:{"title":"my first post"}})
- the second argument is the object that you want to update and the first argument is the filter-
- use `$inc` to add 1 to the reactions
db.books.UpdateOne({"_id":"5e9f9b8f9c8f8c0f8c8f8f8f"},{$inc:{"reactions":1}})
- pull operator to delete the value from the array
db.books.UpdateOne({"_id":"5e9f9b8f9c8f8c0f8c8f8f8f"},{$pull:{"tages":"database"}})
this will delete the value of the tages from the doc that have this `_id ` value.