I recently started learning MongoDB from the Bro Code YouTube channel. This is the list of MongoDB basic terms and it contains the basic commands of MongoDB.
To understand better, we will use the school as an example and work on the school's database which stores student records.
You can use this as a reference when using MongoDB or as a quick revision if you're also learning MongoDB.
Document: Collection of key-value pairs in object
Collection: is a group of Documents
Database: group of Collection
MongoDB commands that you can use in the MongoDB shell:
show dbs
show all databasesuse school
It is the command to use a database named school, it will create one if the school database does not existdb.createCollection("students")
-> Make a new collection in the school databasedb.dropDatabase()
-> Delete the school databasedb.students.insertOne({name:"Spongebob", age:30, gpa:3.5})
if students' collections don't exist, new ones will be created
insertOne() make a new document, we add a javascript object there with data to be inserted
db.students.find()
-> Show all the documentsdb.students.insertMany([{},{},{}])
-> Insert many documents at onceYou need to create Date() to insert date
regDate: new Date()
db.students.find().sort({name:1})
sort in alphabetical orderdb.students.find().sort({name:-1})
sort reverse alphabetical orderdb.students.find().limit(1)
returns only 1 doc (sort by id)db.students.find().sort({gpa:-1}).limit(1)
returns document with the highest gpadb.students.find({name:"Jane doe"})
returns a document with the given namedb.students.find({}, {_id: false, name: true})
returns only names of all documents (set true or false for data you want or don't want)db.students.updateOne({name: "Spongebob"}, {$set: {gpa:1}})
finding a document with the name Spongebob and updating its GPAdb.students.updateOne({name: "Spongebob"}, {$unset: {gpa:""}})
removegpa
property from a document named SpongeBob by setting its value to an empty stringdb.students.updateMany({}, {$set:{gpa:3}})
set all document gpa to 3 (if gpa doesn't exist, one will be created)db.students.updateMany({gpa:{$exists: false}}, {$set:{gpa: 3}})
set gpa to 3 for those documents where gpa doesn't existdb.students.deleteOne({name: "Mike Smith"})
delete a document with the same namedb.students.deleteMany({gpa:3})
delete all documents of gpa:3db.students.find({name: {$ne:"Spongebob"}})
returns all documents whose names do not equal Spongebobdb.students.find({gpa: {$lt:5}})
returns all document which has gpa less than 5 ($lte
= less than or equal to,$gt
= greater than)db.students.find({gpa: {$gt:3, $lt:5}})
returns all document which has gpa between 3 to 5db.students.find({name: {$in:["Spongebob", "Sandy"]}})
returns all documents whose name is one of those two ("Spongebob or Sandy")db.students.find({name: {$nin:["Spongebob", "Sandy"]}})
returns all documents whose name is none of those twodb.students.find({$and: [{name:"Spongebob"}, {gpa:{$lte:3}}]})
returns document that matches both conditionsdb.students.find({$or: [{name: "Spongebob"}, {gpa:{$lte:3}}]})
returns the document even if it matches one of the conditionsdb.students.find({$nor: [{name: "Spongebob"}, {gpa:{$lte:3}}]})
returns the document that doesn't match both conditionsdb.students.find({ gpa: { $not: { $gte: 4}}})
returns all documents that are not greater or equal to 4The index allows quick lookup for the field takes more memories and slows insert, update & removal operations
db.createCollection("teachers")
create new "teachers" collectiondb.createCollection("teacher", {capped:true, size: 10000000, max:100, autoIndexID:false})
If capped is true, you need to specify this collection should have a maximum size.
If you want it 10 MB, give
size: 10000000
Max number of documents is set to 100.
Auto Index id is false
This is the link for the course: Mongodb course.
I am a frontend on my journey to become a full-stack developer and will be learning many web dev technologies and a lot of blogs are on the way this year so stay tuned!
You can connect with me on LinkedIn and Twitter too :)
Thanks for reading!