A Quick Guide to MongoDB Basics

A Quick Guide to MongoDB Basics

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.

  1. Document: Collection of key-value pairs in object

  2. Collection: is a group of Documents

  3. Database: group of Collection

  4. MongoDB commands that you can use in the MongoDB shell:

    1. show dbs show all databases

    2. use school It is the command to use a database named school, it will create one if the school database does not exist

    3. db.createCollection("students") -> Make a new collection in the school database

    4. db.dropDatabase() -> Delete the school database

    5. db.students.insertOne({name:"Spongebob", age:30, gpa:3.5})

      1. if students' collections don't exist, new ones will be created

      2. insertOne() make a new document, we add a javascript object there with data to be inserted

    6. db.students.find() -> Show all the documents

    7. db.students.insertMany([{},{},{}]) -> Insert many documents at once

    8. You need to create Date() to insert date regDate: new Date()

    9. db.students.find().sort({name:1}) sort in alphabetical order

    10. db.students.find().sort({name:-1}) sort reverse alphabetical order

    11. db.students.find().limit(1) returns only 1 doc (sort by id)

    12. db.students.find().sort({gpa:-1}).limit(1) returns document with the highest gpa

    13. db.students.find({name:"Jane doe"}) returns a document with the given name

    14. db.students.find({}, {_id: false, name: true}) returns only names of all documents (set true or false for data you want or don't want)

    15. db.students.updateOne({name: "Spongebob"}, {$set: {gpa:1}}) finding a document with the name Spongebob and updating its GPA

    16. db.students.updateOne({name: "Spongebob"}, {$unset: {gpa:""}}) remove gpa property from a document named SpongeBob by setting its value to an empty string

    17. db.students.updateMany({}, {$set:{gpa:3}}) set all document gpa to 3 (if gpa doesn't exist, one will be created)

    18. db.students.updateMany({gpa:{$exists: false}}, {$set:{gpa: 3}}) set gpa to 3 for those documents where gpa doesn't exist

    19. db.students.deleteOne({name: "Mike Smith"}) delete a document with the same name

    20. db.students.deleteMany({gpa:3}) delete all documents of gpa:3

    21. db.students.find({name: {$ne:"Spongebob"}}) returns all documents whose names do not equal Spongebob

    22. db.students.find({gpa: {$lt:5}}) returns all document which has gpa less than 5 ($lte = less than or equal to, $gt = greater than)

    23. db.students.find({gpa: {$gt:3, $lt:5}}) returns all document which has gpa between 3 to 5

    24. db.students.find({name: {$in:["Spongebob", "Sandy"]}}) returns all documents whose name is one of those two ("Spongebob or Sandy")

    25. db.students.find({name: {$nin:["Spongebob", "Sandy"]}}) returns all documents whose name is none of those two

    26. db.students.find({$and: [{name:"Spongebob"}, {gpa:{$lte:3}}]}) returns document that matches both conditions

    27. db.students.find({$or: [{name: "Spongebob"}, {gpa:{$lte:3}}]}) returns the document even if it matches one of the conditions

    28. db.students.find({$nor: [{name: "Spongebob"}, {gpa:{$lte:3}}]}) returns the document that doesn't match both conditions

    29. db.students.find({ gpa: { $not: { $gte: 4}}}) returns all documents that are not greater or equal to 4

    30. The index allows quick lookup for the field takes more memories and slows insert, update & removal operations

    31. db.createCollection("teachers") create new "teachers" collection

    32. db.createCollection("teacher", {capped:true, size: 10000000, max:100, autoIndexID:false})

      1. If capped is true, you need to specify this collection should have a maximum size.

      2. If you want it 10 MB, give size: 10000000

      3. Max number of documents is set to 100.

      4. 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!