Home / Blog / An Exclusive guide to MongoDB 2024

An Exclusive guide to MongoDB 2024

5 min read.Feb 16, 2024

An Exclusive guide to MongoDB 2024

This guide offers clear explanations and expert insights to optimize their database performance. Stay ahead with cutting-edge techniques and real-world examples, tailored for the modern MongoDB landscape.

Intallation

  • Download and install MongoDb form here
  • Download and install Mongo Shell from here

Connect through Mongo Shell

//Connect to MongoDB Atlas
mongosh "{MONGODB URI}" --apiVersion 1 --username {USERNAME} --password {PASSWORD}

//Connect to local MongoDb
mongosh mongodb://localhost:27017

Create Database

use {DATABASE NAME}

Show Databases

show dbs

Create Collection

db.createCollection(COLLECTION NAME)

Insert Document

//Insert single document
db.collection.insertOne({
    name : 'Dummy Name',
    email: 'dummy@gmail.com'
})

// Insert multiple documents
db.collection.insertMany([
   {
      name : 'Dummy Name 1',
      email: 'dummy1@gmail.com',
      order: 2,
      age: 22,
   },
   {
      name : 'Dummy Name 2',
      email: 'dummy2@gmail.com',
      order: 1,
      age: 18
   }
])

Find Document

//Find single document
 db.collection.findOne()

//Find multiple documents
 db.collection.find()

Update Document

//Update single document
db.collection.updateOne({ email: 'dummy1@gmail.com' }, {name: 'Dummy Updated Name 1'})

//Update multiple documents
db.collection.updateMany({}, {name: 'Dummy Updated Name'}) 

Delete Document

//Delete single document
db.collection.deleteOne({ email: 'dummy1@gmail.com' })

//Delete multiple documents
db.collection.deleteMany({})

Filter Documents

//finding documents that has a value equals to the email  
db.collection.find({email: 'dummy1@gmail.com'})

Sort Documents

// sorting the documents based on age
db.collection.find({}).sort({ age: 1 }) //accending order

db.collection.find({}).sort({ age: -1 }) //decending order

Limit and skip Documents


//This will skip the first 2 documents and return all other doucments
db.collection.find({}).skip(2)

//This will limit the result to 1
db.collection.find({})..limit(1)

//This will skip the first 2 documents and return only 1 document after skipping
db.collection.find({}).skip(2).limit(1)

Count Documents

//count all the documents
db.collection.countDocuments({})

//count all the document having age 18
db.collection.countDocuments({age:18})

Comparison Operators

// $lt -- '<'
// $gt -- '>' 
// $lte -- '<=' 
// $gte -- '>=' 
// $eq -- '='  

db.collection.find({age: {$eq: 18}})

db.collection.find({age: {$gt: 20}})

db.collection.find({age: {$lt: 25}})

db.collection.find({age: {$lte: 20}})

db.collection.find({age: {$gte: 18}})

Logical Operators

// $and -- 'And'
// $or -- 'Or'
// $not -- 'Not'
// $nor -- 'Nor'

// $and operator takes an Array 
// All the conditions must match (i.e. age 18 and order 1)
db.collection.find({
   $and: [
     {
        age: 18
     }, 
     { 
        order: 1
     }
   ]
})

// $or operator takes an Array 
// Any of the conditions should match (i.e. age 18 or order 1)
db.collection.find({
   $or: [
     {
        age: 18
     }, 
     { 
        order: 1
     }
   ]
})

// $nor operator takes an Array 
// All the conditions should fail (i.e. age 18 and order 1)
db.collection.find({
   $nor: [
     {
        age: 18
     }, 
     { 
        order: 1
     }
   ]
})

// $not operator use to give some nagitive conditions
db.collection.find({
   age: {
       $not: {
          $eq: 18  
       }
   }
})

Query Operators

// $in operators takes and array
// Resulted documents should have age those are presents in the array 
db.collection.find({ age: { $in: [18,20] }})

// $nin operators takes and array
// Resulted documents should have age those are not presents in the array 
db.collection.find({ age: { $nin: [18,20] }})

Element Operators

// $exist -- checks a field exits or not
db.collection.find({ age: { $exist: true }})

// $type -- checks a field's data type
db.collection.find({ age: { $type: 'number' }})

Evaluation Operators

// $regex used to match regular expressions
db.collection.find({ name: { $regex: /dummy/, $options: 'i' }})

// $mod used to match the reminder after division
// after deviding age by 2 reminder should be 1 
db.collection.find({ age: { $mod: [2, 1] }}) 

Indexing and Searching

// Indexes are helpful to query inside mongodb efficiently
// Create Index
db.collection.createIndex({ name: "text", email: "text" })

// $text and $search to search documents using those indexes
db.collection.find({ 
    $text : {
        $search: 'dummy'
    } 
})