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
Connect through Mongo Shell
1 //Connect to MongoDB Atlas
2 mongosh "{MONGODB URI}" --apiVersion 1 --username {USERNAME} --password {PASSWORD}
3
4 //Connect to local MongoDb
5 mongosh mongodb://localhost:27017
Create Database
1 use {DATABASE NAME}
Show Databases
1 show dbs
Create Collection
1 db.createCollection(COLLECTION NAME)
Insert Document
1 //Insert single document
2 db.collection.insertOne({
3 name : 'Dummy Name',
4 email: 'dummy@gmail.com'
5 })
6
7 // Insert multiple documents
8 db.collection.insertMany([
9 {
10 name : 'Dummy Name 1',
11 email: 'dummy1@gmail.com',
12 order: 2,
13 age: 22,
14 },
15 {
16 name : 'Dummy Name 2',
17 email: 'dummy2@gmail.com',
18 order: 1,
19 age: 18
20 }
21 ])
Find Document
1 //Find single document
2 db.collection.findOne()
3
4 //Find multiple documents
5 db.collection.find()
6
Update Document
1 //Update single document
2 db.collection.updateOne({ email: 'dummy1@gmail.com' }, {name: 'Dummy Updated Name 1'})
3
4 //Update multiple documents
5 db.collection.updateMany({}, {name: 'Dummy Updated Name'})
Delete Document
1 //Delete single document
2 db.collection.deleteOne({ email: 'dummy1@gmail.com' })
3
4 //Delete multiple documents
5 db.collection.deleteMany({})
Filter Documents
1 //finding documents that has a value equals to the email
2 db.collection.find({email: 'dummy1@gmail.com'})
3
Sort Documents
1 // sorting the documents based on age
2 db.collection.find({}).sort({ age: 1 }) //accending order
3
4 db.collection.find({}).sort({ age: -1 }) //decending order
Limit and skip Documents
1
2 //This will skip the first 2 documents and return all other doucments
3 db.collection.find({}).skip(2)
4
5 //This will limit the result to 1
6 db.collection.find({})..limit(1)
7
8 //This will skip the first 2 documents and return only 1 document after skipping
9 db.collection.find({}).skip(2).limit(1)
10
Count Documents
1 //count all the documents
2 db.collection.countDocuments({})
3
4 //count all the document having age 18
5 db.collection.countDocuments({age:18})
Comparison Operators
1 // $lt -- '<'
2 // $gt -- '>'
3 // $lte -- '<='
4 // $gte -- '>='
5 // $eq -- '='
6
7 db.collection.find({age: {$eq: 18}})
8
9 db.collection.find({age: {$gt: 20}})
10
11 db.collection.find({age: {$lt: 25}})
12
13 db.collection.find({age: {$lte: 20}})
14
15 db.collection.find({age: {$gte: 18}})
16
Logical Operators
1 // $and -- 'And'
2 // $or -- 'Or'
3 // $not -- 'Not'
4 // $nor -- 'Nor'
5
6 // $and operator takes an Array
7 // All the conditions must match (i.e. age 18 and order 1)
8 db.collection.find({
9 $and: [
10 {
11 age: 18
12 },
13 {
14 order: 1
15 }
16 ]
17 })
18
19 // $or operator takes an Array
20 // Any of the conditions should match (i.e. age 18 or order 1)
21 db.collection.find({
22 $or: [
23 {
24 age: 18
25 },
26 {
27 order: 1
28 }
29 ]
30 })
31
32 // $nor operator takes an Array
33 // All the conditions should fail (i.e. age 18 and order 1)
34 db.collection.find({
35 $nor: [
36 {
37 age: 18
38 },
39 {
40 order: 1
41 }
42 ]
43 })
44
45 // $not operator use to give some nagitive conditions
46 db.collection.find({
47 age: {
48 $not: {
49 $eq: 18
50 }
51 }
52 })
53
54
Query Operators
1 // $in operators takes and array
2 // Resulted documents should have age those are presents in the array
3 db.collection.find({ age: { $in: [18,20] }})
4
5 // $nin operators takes and array
6 // Resulted documents should have age those are not presents in the array
7 db.collection.find({ age: { $nin: [18,20] }})
Element Operators
1 // $exist -- checks a field exits or not
2 db.collection.find({ age: { $exist: true }})
3
4 // $type -- checks a field's data type
5 db.collection.find({ age: { $type: 'number' }})
Evaluation Operators
1 // $regex used to match regular expressions
2 db.collection.find({ name: { $regex: /dummy/, $options: 'i' }})
3
4 // $mod used to match the reminder after division
5 // after deviding age by 2 reminder should be 1
6 db.collection.find({ age: { $mod: [2, 1] }})
Indexing and Searching
1 // Indexes are helpful to query inside mongodb efficiently
2 // Create Index
3 db.collection.createIndex({ name: "text", email: "text" })
4
5 // $text and $search to search documents using those indexes
6 db.collection.find({
7 $text : {
8 $search: 'dummy'
9 }
10 })
11
12