MongoDB cheatsheet for Beginners
author image

Author:

Naman AroraWed, 17 Jan, 2024

Welcome to our comprehensive MongoDB cheatsheet designed to empower MongoDB beginners with essential commands. Whether you're navigating databases, collections, or rows (documents), this guide covers the commands you need to kickstart your MongoDB journey.

let's familiarize ourselves with some key MongoDB concepts:

  • Database: A database in MongoDB is a container that holds collections. It's the top-level storage component, similar to a database in traditional relational database systems.
  • Collection: Collections in MongoDB are analogous to tables in relational databases. However, they offer more flexibility as each document within a collection can have different fields.
  • Document: A document is a record in a MongoDB collection. It's the basic unit of data, represented as a BSON (Binary JSON) object. Think of it as similar to a row in a relational database.

Now, armed with a basic understanding of these terms, let's explore the essential commands you'll need to kickstart your MongoDB journey.

1. Database Commands

View all databases

show dbs

Create a new or switch databases

use dbName

View current Database

db

Delete Database

db.dropDatabase()

2. Collection Commands

Show Collections

show collections

Create a collection named 'collector'

db.createCollection('collector')

Drop a collection named 'collector'

db.collector.drop()

3. Row (Document) Commands

Show all Rows in a Collection

db.collector.find()

Show all Rows in a Collection (Prettified)

db.collector.find().pretty()

Find the first row matching the object

db.collector.findOne({name: 'Naman'})

Insert One Row

db.collector.insert({
    'name': 'Naman',
    'lang': 'TypeScript',
    'member_since': 5
 })

Insert many Rows

db.collector.insertMany([{
    'name': 'Naman',
    'lang': 'TypeScript',
    'member_since': 5
    }, 
    {'name': 'Rohan',
    'lang': 'Python',
    'member_since': 3
    },
    {'name': 'Lovish',
    'lang': 'Java',
    'member_since': 4
}])

Search in a MongoDb Database

db.collector.find({lang:'Python'})

Limit the number of rows in output

db.collector.find().limit(2)

Count the number of rows in the output

db.collector.find().count()

Update a row

db.collector.updateOne({name: 'Shubham'},
{$set: {'name': 'Naman',
    'lang': 'TypeScript',
    'member_since': 51
}}, {upsert: true})

Mongodb Increment Operator

db.collector.update({name: 'Rohan'},
{$inc:{
    member_since: 2
}})

Mongodb Rename Operator

db.collector.update({name: 'Rohan'},
{$rename:{
    member_since: 'member'
}})

Delete Row

db.collector.remove({name: 'Naman'})

Less than/Greater than/ Less than or Eq/Greater than or Eq

db.collector.find({member_since: {$lt: 90}})

db.collector.find({member_since: {$lte: 90}})

db.collector.find({member_since: {$gt: 90}})

db.collector.find({member_since: {$gte: 90}})

Conclusion

And there you have it—a comprehensive MongoDB cheatsheet for beginners. Armed with these commands, you're ready to navigate databases, collections, and documents with confidence. Dive into your MongoDB journey, practice these commands.

Happy Coding!

author image

Naman Arora

Technical Author with a passion for translating the complexities of software, computers, and emerging technologies into accessible and engaging content. Armed with a background in computer science, I blend technical expertise with a flair for effective communication in my writing. Join me on this tech-savvy journey as we explore coding languages, unravel the nuances of software architecture, and stay informed about the latest tech trends. Let's navigate the digital frontier together!

Portfolio designed and developed by Naman Arora