-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo-DB.html
836 lines (776 loc) · 40.9 KB
/
mongo-DB.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>NoSQL</title>
<meta content="" name="description">
<meta content="" name="keywords">
<!-- Favicons -->
<link href="assets/img/Favicon-1.png" rel="icon">
<link href="assets/img/Favicon-1.png" rel="apple-touch-icon">
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Raleway:300,300i,400,400i,500,500i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">
<!-- Vendor CSS Files -->
<link href="assets/vendor/aos/aos.css" rel="stylesheet">
<link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="assets/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet">
<link href="assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
<link href="assets/vendor/glightbox/css/glightbox.min.css" rel="stylesheet">
<link href="assets/vendor/swiper/swiper-bundle.min.css" rel="stylesheet">
<!-- Creating a python code section-->
<link rel="stylesheet" href="assets/css/prism.css">
<script src="assets/js/prism.js"></script>
<!-- Template Main CSS File -->
<link href="assets/css/style.css" rel="stylesheet">
<!-- To set the icon, visit https://fontawesome.com/account-->
<script src="https://kit.fontawesome.com/5d25c1efd3.js" crossorigin="anonymous"></script>
<!-- end of icon-->
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<style>
/* Add some basic styling for code */
pre {
background-color: #f4f4f4;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
font-family: monospace;
white-space: pre-wrap;
}
</style>
<!-- =======================================================
* Template Name: iPortfolio
* Updated: Sep 18 2023 with Bootstrap v5.3.2
* Template URL: https://bootstrapmade.com/iportfolio-bootstrap-portfolio-websites-template/
* Author: BootstrapMade.com
* License: https://bootstrapmade.com/license/
======================================================== -->
</head>
<body>
<!-- ======= Mobile nav toggle button ======= -->
<i class="bi bi-list mobile-nav-toggle d-xl-none"></i>
<!-- ======= Header ======= -->
<header id="header">
<div class="d-flex flex-column">
<div class="profile">
<img src="assets/img/myphoto.jpeg" alt="" class="img-fluid rounded-circle">
<h1 class="text-light"><a href="index.html">Arun</a></h1>
<div class="social-links mt-3 text-center">
<a href="https://www.linkedin.com/in/arunp77/" target="_blank" class="linkedin"><i class="bx bxl-linkedin"></i></a>
<a href="https://github.com/arunp77" target="_blank" class="github"><i class="bx bxl-github"></i></a>
<a href="https://twitter.com/arunp77_" target="_blank" class="twitter"><i class="bx bxl-twitter"></i></a>
<a href="https://www.instagram.com/arunp77/" target="_blank" class="instagram"><i class="bx bxl-instagram"></i></a>
<a href="https://arunp77.medium.com/" target="_blank" class="medium"><i class="bx bxl-medium"></i></a>
</div>
</div>
<nav id="navbar" class="nav-menu navbar">
<ul>
<li><a href="index.html#hero" class="nav-link scrollto active"><i class="bx bx-home"></i> <span>Home</span></a></li>
<li><a href="index.html#about" class="nav-link scrollto"><i class="bx bx-user"></i> <span>About</span></a></li>
<li><a href="index.html#resume" class="nav-link scrollto"><i class="bx bx-file-blank"></i> <span>Resume</span></a></li>
<li><a href="index.html#portfolio" class="nav-link scrollto"><i class="bx bx-book-content"></i> <span>Portfolio</span></a></li>
<li><a href="index.html#skills-and-tools" class="nav-link scrollto"><i class="bx bx-wrench"></i> <span>Skills and Tools</span></a></li>
<!-- <li><a href="index.html#services" class="nav-link scrollto"><i class="bx bx-server"></i> <span>Services</span></a></li>-->
<li><a href="index.html#professionalcourses" class="nav-link scrollto"><i class="bx bx-book-alt"></i> <span>Professional Certification</span></a></li>
<li><a href="index.html#publications" class="nav-link scrollto"><i class="bx bx-news"></i> <span>Publications</span></a></li>
<li><a href="index.html#extra-curricular" class="nav-link scrollto"><i class="bx bx-rocket"></i> <span>Extra-Curricular Activities</span></a></li>
<li><a href="index.html#contact" class="nav-link scrollto"><i class="bx bx-envelope"></i> <span>Contact</span></a></li>
</ul>
</nav><!-- .nav-menu -->
</div>
</header><!-- End Header -->
<main id="main">
<!-- ======= Breadcrumbs ======= -->
<section id="breadcrumbs" class="breadcrumbs">
<div class="container">
<div class="d-flex justify-content-between align-items-center">
<h2>Data Engineering</h2>
<ol>
<li><a href="Data-engineering.html" class="clickable-box">Content section</a></li>
<li><a href="index.html#portfolio" class="clickable-box">Portfolio section</a></li>
</ol>
</div>
</div>
</section><!-- End Breadcrumbs -->
<!------ right dropdown menue ------->
<div class="right-side-list">
<div class="dropdown">
<button class="dropbtn"><strong>Shortcuts:</strong></button>
<div class="dropdown-content">
<ul>
<li><a href="cloud-compute.html"><i class="fas fa-cloud"></i> Cloud</a></li>
<li><a href="AWS-GCP.html"><i class="fas fa-cloud"></i> AWS-GCP</a></li>
<li><a href="amazon-s3.html"><i class="fas fa-cloud"></i> AWS S3</a></li>
<li><a href="ec2-confi.html"><i class="fas fa-server"></i> EC2</a></li>
<li><a href="Docker-Container.html"><i class="fab fa-docker" style="color: rgb(29, 27, 27);"></i> Docker</a></li>
<li><a href="Jupyter-nifi.html"><i class="fab fa-python" style="color: rgb(34, 32, 32);"></i> Jupyter-nifi</a></li>
<li><a href="snowflake-task-stream.html"><i class="fas fa-snowflake"></i> Snowflake</a></li>
<li><a href="data-model.html"><i class="fas fa-database"></i> Data modeling</a></li>
<li><a href="sql-basics.html"><i class="fas fa-table"></i> QL</a></li>
<li><a href="sql-basic-details.html"><i class="fas fa-database"></i> SQL</a></li>
<li><a href="Bigquerry-sql.html"><i class="fas fa-database"></i> Bigquerry</a></li>
<li><a href="scd.html"><i class="fas fa-archive"></i> SCD</a></li>
<li><a href="sql-project.html"><i class="fas fa-database"></i> SQL project</a></li>
<!-- Add more subsections as needed -->
</ul>
</div>
</div>
</div>
<!-- ======= Portfolio Details Section ======= -->
<section id="portfolio-details" class="portfolio-details">
<div class="container">
<div class="row gy-4">
<h1>NOSQL & MongoDB</h1>
<div class="col-lg-8">
<div class="portfolio-details-slider swiper">
<div class="swiper-wrapper align-items-center">
<figure>
<img src="assets/img/data-engineering/sql-logo.png" alt="" style="max-width: 80%; max-height: auto;">
<figcaption></figcaption>
</figure>
</div>
<div class="swiper-pagination"></div>
</div>
</div>
<div class="col-lg-4 grey-box">
<div class="section-title">
<h3>Content</h3>
<ol>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#reference">Reference</a></li>
</ol>
</div>
</div>
</div>
<section id="introduction">
<h3>Introduction</h3>
NoSQL, which stands for "Not Only SQL," is a broad term for a variety of database technologies that are not based on the traditional relational model used in SQL databases. NoSQL databases are designed to store and manage large amounts of unstructured or semi-structured data, which is not well-suited for relational databases.
<h5>Characheristics:</h5>
Some of the key characteristics of NoSQL databases include:
<ul>
<li><strong>Schemaless: </strong>NoSQL databases do not require a predefined schema, which means that the data structure can evolve over time. This is in contrast to relational databases,
which require a rigid schema that is defined in advance.</li>
<li><strong>Flexible data structures: </strong>NoSQL databases support a variety of data structures, such as key-value pairs, documents, and graphs. This flexibility allows for more
efficient storage and retrieval of different types of data.</li>
<li><strong>Horizontal scalability: </strong>NoSQL databases are designed to be horizontally scalable, which means that they can be easily scaled by adding more nodes to the cluster.
This is in contrast to relational databases, which are typically vertically scaled by adding more resources to a single server.</li>
</ul>
NoSQL databases are often used for applications that require the storage and management of large amounts of unstructured or semi-structured data, such as social media data, e-commerce data,
and IoT data. Some popular NoSQL databases include MongoDB, Cassandra, and Couchbase
<p></p>
<h5>Key differences between the SQL databases and NoSQL databases:</h5>
<table>
<tr>
<th>Feature</th>
<th>SQL Databases</th>
<th>NoSQL Databases</th>
</tr>
<tr>
<td>Schema</td>
<td>Rigid, predefined</td>
<td>Schemaless, flexible</td>
</tr>
<tr>
<td>Data structure</td>
<td>Tables with rows and columns</td>
<td>Key-value pairs, documents, graphs</td>
</tr>
<tr>
<td>Scalability</td>
<td>Vertical, limited by single server</td>
<td>Horizontal, scalable by adding node</td>
</tr>
<tr>
<td>Use cases</td>
<td>Structured data, traditional applications</td>
<td>Unstructured or semi-structured data, modern applications</td>
</tr>
</table><br>
<h4>Types of NoSQL Databases:</h4>
<ul>
<li><strong>Document-Oriented: </strong> Stores data in flexible, JSON-like documents. Examples include MongoDB and CouchDB.
Each document contains key-value pairs, and collections group related documents.
<pre>
Collection 1
Document 1
{
"field1": "value1",
"field2": "value2",
"field3": "value3"
}
Document 2
{
"field1": "value4",
"field2": "value5",
"field3": "value6"
}
Collection 2
Document 1
{
"field1": "value7",
"field2": "value8",
"field3": "value9"
}
</pre>
<strong>Most dommon data strunctures: </strong>
<ul>
<li><strong>JSON or BSON Documents: </strong>Stores data in a flexible, hierarchical format.
<strong>Example: </strong>
<pre>
# Json file
{
"_id": 1,
"name": "John Doe",
"age": 30,
"address": {
"street": "123 Main St",
"city": "Exampleville",
"country": "Exampleland"
}
}
</pre>
</li>
<li><strong>Collections: </strong>Groups related documents together for easier organization.
<strong>Example: </strong>
<pre>
// Users Collection
{
"_id": 1,
"name": "John Doe",
"age": 30
}
// Orders Collection
{
"_id": 101,
"user_id": 1,
"total_amount": 50.00
}
</pre>
</li>
</ul>
</li>
<li><strong>Key-Value Stores: </strong>Simple key-value pairs for data storage. Redis and DynamoDB are examples. In this case data is stored as simple key-value pairs.
<pre>
Key: "user:1"
Value: "John Doe"
Key: "product:42"
Value: "{ name: 'Product A', price: 29.99 }"
</pre>
<strong>Most dommon data strunctures: </strong>
<ul>
<li><strong>Hashtable: </strong>Uses keys to directly access data, providing fast retrieval.
<strong>Example: </strong>
<pre>
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
</pre>
</li>
<li><strong>B-trees: </strong>Efficiently organizes key-value pairs for range queries and sequential access.
<strong>Example: </strong>
<pre>
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
</pre>
</li>
</ul>
</li>
<li><strong>Column-Family Stores: </strong>Organizes data into columns rather than rows. Apache Cassandra is a popular choice.</li>
<li><strong>Graph Databases: </strong> Designed for storing and querying graph-like data structures. Neo4j is a well-known graph
database. In this case data is represented as nodes and relationships between nodes.
<pre>
(Node) User1
- Knows
(Node) User2
- Likes
(Node) Product1
(Node) User3
- Follows
(Node) User1
</pre>
<strong>Most dommon data strunctures: </strong>
<ul>
<li><strong>Adjacency List: </strong>Represents graph as a collection of vertices and their adjacent vertices.
<strong>Example: </strong>
<pre>
{
"A": ["B", "C"],
"B": ["A", "D"],
"C": ["A"],
"D": ["B"]
}
</pre>
</li>
<li><strong>Adjacency Matrix: </strong>Stores relationships between vertices in a matrix format.
<strong>Example: </strong>
<pre>
A B C D
A 0 1 1 0
B 1 0 0 1
C 1 0 0 0
D 0 1 0 0
</pre>
</li>
</ul>
</li>
</ul>
<br>
<table>
<tr>
<th style="background-color: rgb(81, 163, 152);">Feature</th>
<th>Key-value stores</th>
<th>Graph databases</th>
<th>Document-oriented databases</th>
</tr>
<tr>
<td style="background-color: rgb(184, 236, 229);">Data structure</td>
<td>Key-value pairs</td>
<td>Nodes, edges</td>
<td>Documents</td>
</tr>
<tr>
<td style="background-color: rgb(184, 236, 229);">flexibility</td>
<td>Moderate</td>
<td>Limited</td>
<td>Flexible</td>
</tr>
<tr>
<td style="background-color: rgb(184, 236, 229);">Indexing</td>
<td>Simple indexing</td>
<td>Complex indexing</td>
<td>Efficient indexing</td>
</tr>
<tr>
<td style="background-color: rgb(184, 236, 229);">Search</td>
<td>Simple keyword search</td>
<td>Advanced graph search</td>
<td>Flexible search</td>
</tr>
</table><br>
<!-------------------->
<h4>Use cases:</h4>
<ul>
<li>NoSQL databases are often used in scenarios where there's a need for quick and agile development.</li>
<li>They excel in handling large volumes of data with varying structures.</li>
<li>Commonly applied in real-time big data applications and analytics.</li>
</ul>
<!----------------->
<h4>Popular NoSQL Databases:</h4>
<ul>
<li><strong>MongoDB:</strong>Document-oriented and widely used for its scalability and flexibility.</li>
<li><strong>Cassandra: </strong>Column-family store known for its high availability and fault tolerance.</li>
<li><strong>Redis: </strong>Key-value store with in-memory data storage.</li>
<li><strong>Neo4j: </strong>A graph database for handling interconnected data.</li>
</ul>
<!----------------->
<h4>How does MongoDB work?</h4>
MongoDB environments offer a server for creating databases, where data is stored as records composed of collections and documents.
Documents, the basic data units, consist of field-value pairs and use Binary JSON (BSON) format for flexibility in data types.
Fields are similar to relational database columns, and documents may include various data types. Collections, akin to tables, group
sets of documents within a database. The mongo shell, an interactive JavaScript interface, facilitates querying, updating, and
administrative operations.
BSON provides a binary representation of JSON-like documents, enhancing data type accommodation. Automatic sharding enables horizontal
scalability by distributing data across multiple systems. MongoDB utilizes a single master architecture for data consistency, with secondary
databases maintaining copies for automatic failover through replication.
<!----------------->
<h4>Why is MongoDB used?</h4>
An organization might want to use MongoDB for the following
<ul>
<li><strong>Storage: </strong>MongoDB can store large structured and unstructured data volumes and is scalable vertically and horizontally.
Indexes are used to improve search performance. Searches are also done by field, range and expression queries.</li>
<li><strong>Data integration: </strong>This integrates data for applications, including for hybrid and multi-cloud applications.</li>
<li><strong>Complex data structures descriptions: </strong>Document databases enable the embedding of documents to describe nested
structures (a structure within a structure) and can tolerate variations in data.</li>
<li><strong>Load balancing: </strong>MongoDB can be used to run over multiple servers.</li>
</ul>
<!----------------->
<h4>Features of MongoDB</h4>
Features of MongoDB include the following:
<ul>
<li><strong>Replication: </strong>A replica set is two or more MongoDB instances used to provide high availability. Replica sets are made
of primary and secondary servers. The primary MongoDB server performs all the read and write operations, while the secondary replica
keeps a copy of the data. If a primary replica fails, the secondary replica is then used.</li>
<li><strong>Scalability: </strong>MongoDB supports vertical and horizontal scaling. Vertical scaling works by adding more power to an
existing machine, while horizontal scaling works by adding more machines to a user's resources.</li>
<li><strong>Load balancing: </strong>MongoDB handles load balancing without the need for a separate, dedicated load balancer, through either vertical or horizontal scaling.</li>
<li><strong>Schema-less: </strong>MongoDB is a schema-less database, which means the database can manage data without the need for a blueprint.</li>
<li><strong>Document: </strong>Data in MongoDB is stored in documents with key-value pairs instead of rows and columns, which makes the data more
flexible when compared to SQL databases.</li>
</ul>
<!---------------->
<h4>Advantages of MongoDB</h4>
MongoDB offers several potential benefits:
<ul>
<li><strong>Schema-less: </strong> Like other NoSQL databases, MongoDB doesn't require predefined schemas. It stores any type of data. This gives users the flexibility to create
any number of fields in a document, making it easier to scale MongoDB databases compared to relational databases.</li>
<li><strong>Document-oriented: </strong> One of the advantages of using documents is that these objects map to native data types in several programming languages.,
Having embedded documents also reduces the need for database joins, which can lower costs.</li>
<li><strong>Scalability:</strong> A core function of MongoDB is its horizontal scalability, which makes it a useful database for companies running big data applications. In addition,
sharding lets the database distribute data across a cluster of machines. MongoDB also supports the creation of zones of data based on a shard key.</li>
<li><strong>Third-party support: </strong> MongoDB supports several storage engines and provides pluggable storage engine APIs that let third parties develop their own storage engines for MongoDB.</li>
<li><strong>Aggregation: </strong> The DBMS also has built-in aggregation capabilities, which lets users run MapReduce code directly on the database rather than running MapReduce on
Hadoop. MongoDB also includes its own file system called GridFS, akin to the Hadoop Distributed File System. The use of the file system is primarily for storing files larger than BSON's
size limit of 16 MB per document. These similarities let MongoDB be used instead of Hadoop, though the database software does integrate with Hadoop, Spark and other data processing frameworks.</li>
</ul>
<!----------------------->
<h5>Disadvantages of MongoDB</h5>
Though there are some valuable benefits to MongoDB, there are some downsides to it as well.
<ul>
<li><strong>Continuity.: </strong> With its automatic failover strategy, a user sets up just one master node in a MongoDB cluster. If the master fails, another node will automatically convert to the
new master. This switch promises continuity, but it isn't instantaneous -- it can take up to a minute. By comparison, the Cassandra NoSQL database supports multiple master nodes. If one master goes
down, another is standing by, creating a highly available database infrastructure.</li>
<li><strong>Write limits: </strong> MongoDB's single master node also limits how fast data can be written to the database. Data writes must be recorded on the master, and writing new information to
the database is limited by the capacity of that master node.</li>
<li><strong>Data consistency: </strong> MongoDB doesn't provide full referential integrity through the use of foreign-key constraints, which could affect data consistency.</li>
<li><strong>Security: </strong> In addition, user authentication isn't enabled by default in MongoDB databases. However, malicious hackers have targeted large numbers of unsecured MongoDB systems in
attacks, which led to the addition of a default setting that blocks networked connections to databases if they haven't been configured by a database administrator.</li>
</ul>
<!---------------------->
<h3>MongoDB query language (MQL):</h3>
MongoDB uses a query language called MongoDB Query Language (MQL) for interacting with its databases. MQL provides a set of operators and methods for querying, updating, and manipulating data within
MongoDB collections. Here are some detailed MQL queries for common operations (assume that our collection name is: <code>collection_db</code>):
<ol>
<li><strong>Basics of Querying: </strong>
<ul>
<li><strong>Find Documents: </strong>
<pre class="language-sql"><code>db.collection_db.find({ key: value });</code></pre>
</li>
<strong>Example: </strong>
<pre class="language-sql"><code>db.students.find({ grade: "A" });</code></pre>
<strong>Example:</strong>
<pre class="language-sql"><code>
// Example: Search for documents containing the word "MongoDB"
db.articles.find({ $text: { $search: "MongoDB" } });
</code></pre>
<strong>Example:</strong>
<pre class="language-sql"><code>
// Example: Find documents where the array "scores" has at least one element greater than 90
db.students.find({ "scores": { $elemMatch: { $gt: 90 } } });
</code></pre>
<li><strong>Specify Multiple Conditions:</strong>
<pre class="language-sql"><code>db.collection_db.find({ key1: value1, key2: value2 });</code></pre>
</li>
<li><strong>Query with Operators:</strong>
<pre class="language-sql"><code>db.collection_db.find({ age: { $gt: 25 } });</code></pre>
</li>
</ul>
</li>
<li><strong>Projection: </strong>
<ul>
<li><strong>Select Fields to Return:</strong>
<pre class="language-sql"><code>db.collection_db.find({}).project({ key1: 1, key2: 1 });</code></pre>
</li>
<li><strong>Exclude Fields:</strong>
<pre class="language-sql"><code>db.collection_db.find({}).project({ keyToExclude: 0 });</code></pre>
</li>
</ul>
</li>
<li><strong>Sorting: </strong>
<ul>
<li><strong>Sort in Ascending Order:</strong>
<pre class="language-sql"><code>db.collection_db.find({}).sort({ key: 1 });</code></pre>
</li>
<li><strong>Sort in Descending Order:</strong>
<pre class="language-sql"><code>db.collection_db.find({}).sort({ key: -1 });</code></pre>
</li>
</ul>
</li>
<li><strong>Limit and skips: </strong>
<ul>
<li><strong>Limit Results:</strong>
<pre class="language-sql"><code>db.collection_db.find({}).limit(10);</code></pre>
</li>
<li><strong>Skip Results:</strong>
<pre class="language-sql"><code>db.collection_db.find({}).skip(5);</code></pre>
</li>
</ul>
</li>
<li><strong>Update Documents: </strong> To update a document in a collection:
<ul>
<li><strong>Update a Single Document:</strong>
<pre class="language-sql"><code>db.collection_db.updateOne({ filter }, { $set: { key: value } });</code></pre>
</li>
<strong>Example:</strong>
<pre class="language-sql"><code>db.students.updateOne(
{ name: "John Doe" },
{
$set: : {
age: 28
}
});</code></pre>
<li><strong>Update Multiple Documents:</strong>
<pre class="language-sql"><code>db.collection_db.updateMany(
{ filter },
{
$set: {
key: value
}
});</code></pre>
</li>
</ul>
</li>
<li><strong>Delete Documents: </strong>
<ul>
<li><strong>Delete a Single Document: </strong>
<pre class="language-sql"><code>db.collection_db.deleteOne({ filter });</code></pre>
</li>
<strong>Example:</strong>
<pre class="language-sql"><code>db.students.deleteOne({ name: "John Doe", });</code></pre>
<li><strong>Delete Multiple Documents:</strong>
<pre class="language-sql"><code>db.students.deleteOne({ filter });</code></pre>
</li>
</ul>
</li>
<li><strong>Aggregration frameworks: </strong>
<ul>
<li><strong>Aggregate Data:</strong>
<pre class="language-sql"><code>
db.collection_db.aggregate([
{ $match: { key: value } },
{ $group: { _id: "$key", count: { $sum: 1 } } },
]);
</code></pre>
<strong>Example:</strong>
</li>
<pre class="language-sql"><code>
// Example: Calculate the average age of students in each grade
db.students.aggregate([
{ $group: { _id: "$grade", avgAge: { $avg: "$age" } } },
{ $sort: { avgAge: -1 } },
]);
</code></pre>
</ul>
</li>
<li><strong>Indexes: </strong>
<ul>
<li><strong>Create an Index:</strong>
<pre class="language-sql"><code>db.collection_db.createIndex({ key: 1 });</code></pre>
</li>
<li><strong>Explain Query Execution Plan:</strong>
<pre class="language-sql"><code>db.collection_db.find({ key: value }).explain("executionStats");</code></pre>
</li>
</ul>
</li>
<li><strong>Insert Docuemnt:</strong> To insert a document into a collection:
<pre class="language-sql"><code>db.collection_db.insertOne({key1 : value1, key2 : value2})</code></pre>
</li>
<strong>Example:</strong>
<pre class="language-sql"><code>db.students.insertOne({name: "John Doe", age: 25, grade: "A",})</code></pre>
</ol>
<!------------------->
<h3>Advanced MQL queris:</h3>
Advanced MongoDB Query Language (MQL) queries can involve more complex operations, aggregations, and various stages in the aggregation pipeline. Below are some examples of advanced MQL queries:
<ol>
<li><strong>Operators:</strong> MongoDB operators are used in queries to perform various operations on documents. Some common operators include:
<ul>
<li><strong>Comparison Operators:</strong></li>
<table>
<tr>
<th>Operator</th>
<th>Description:</th>
</tr>
<tr>
<td><code>$eq</code> </td>
<td> Matches values that are equal to a specified value.</td>
</tr>
<tr>
<td><code>$ne</code></td>
<td>Matches values that are not equal to a specified value.</td>
</tr>
<tr>
<td><code>$gt</code>, <code>$gte</code></td>
<td>Matches values that are greater than (or equal to) a specified value.</td>
</tr>
<tr>
<td><code>$lt</code>, <code>$lte</code></td>
<td>Matches values that are less than (or equal to) a specified value.</td>
</tr>
</table>
<li><strong>Logical Operators:</strong></li>
<table>
<tr>
<th>Operator</th>
<th>Description:</th>
</tr>
<tr>
<td><code>%and</code>, <code>$or</code>, <code>$not</code></td>
<td>Perform logical AND, OR, and NOT operations, respectively.</td>
</tr>
</table>
<li><strong>Element Operators:</strong></li>
<table>
<tr>
<th>Operator</th>
<th>Description:</th>
</tr>
<tr>
<td><code>$exists</code></td>
<td>Matches documents that have the specified field.</td>
</tr>
<tr>
<td><code>$type</code></td>
<td>Selects documents if a field is of the specified type.</td>
</tr>
</table>
<li><strong>Array Operators:</strong></li>
<table>
<tr>
<th>Operator</th>
<th>Description:</th>
</tr>
<tr>
<td><code>$in</code></td>
<td>Matches any of the values specified in an array.</td>
</tr>
<tr>
<td><code>$all</code></td>
<td>Matches arrays that contain all elements specified in the query.</td>
</tr>
</table>
</ul>
<pre class="language-sql"><code>
// Find documents where age is greater than 25 and grade is "A"
db.students.find({ $and: [{ age: { $gt: 25 } }, { grade: "A" }] });
</code></pre>
</li>
<li><strong>Cursor Methods:</strong>
MongoDB provides cursor methods to interact with the result set returned by queries. Common cursor methods include:
<table>
<tr>
<th>Methods</th>
<th>Descriptions</th>
</tr>
<tr>
<td><code>limit(n)</code></td>
<td>Limits the number of documents returned.</td>
</tr>
<tr>
<td><code>skip(n)</code></td>
<td>Skips the first <code>n</code> documents.</td>
</tr>
<tr>
<td><code>sort({ field: 1})</code></td>
<td>Sorts the result set in ascending order based on the specified field. Use <code>-1</code> for descending order.</td>
</tr>
<tr>
<td><code>count()</code></td>
<td> Returns the count of documents in the result set.</td>
</tr>
<tr>
<td><code>forEach(fn)</code></td>
<td>Applies a JavaScript function for each document in the result set.</td>
</tr>
</table>
<strong>Example:</strong>
<pre class="language-sql"><code>
// Find and limit to 10 documents, skip the first 5, and sort by age in descending order
db.students.find().limit(10).skip(5).sort({ age: -1 });
</code></pre>
</li>
<li><strong>Projections</strong>
Projections in MongoDB are used to control which fields are returned in the query result. The <code>project</code> method or the second argument of the <code>find</code> method is used for projections.
<ul>
<li><strong>Include Fields:</strong></li>
<pre class="language-sql"><code>
// Only include the name and age fields
db.students.find({}, { name: 1, age: 1, _id: 0 });
</code></pre>
<li><strong>Exclude Fields:</strong></li>
<pre class="language-sql"><code>
// Exclude the grade field
db.students.find({}, { grade: 0 });
</code></pre>
Projections help in reducing the amount of data sent over the network and can improve query performance.
</ul>
</li>
</ol>
</section>
<!---------------------->
<h3>CRUD operations</h3>
In MongoDB, CRUD stands for Create, Read, Update, and Delete – the basic operations that can be performed on documents in a collection. Here's a brief overview of each CRUD operation in the context of MongoDB:
<ol>
<li><strong>Create (C):</strong> To insert a new document into a collection, you use the <code>insertOne()</code> or <code>insertMany()</code> method.
<pre class="language-sql"><code>
db.collection.insertOne({ name: "John Doe", age: 30, city: "Example City" });
</code></pre>
</li>
<li><strong>Read(R):</strong> To retrieve documents from a collection, you use the <code>find()</code> method. You can specify criteria for filtering results.
<pre class="language-sql"><code>
db.collection.find({ age: { $gt: 25 } });
</code></pre>
</li>
<li><strong>Update (U):</strong> To modify existing documents, you use the <code>updateOne()</code> or <code>updateMany()</code> method. You can specify criteria for matching and define the changes using update operators.
<pre class="language-sql"><code>
db.collection.updateOne({ name: "John Doe" }, { $set: { age: 31 } });
</code></pre>
</li>
<li><strong>Delte (D):</strong> To remove documents from a collection, you use the <code>deleteOne()</code> or <code>deleteMany()</code> method. You specify criteria for matching documents to be deleted.
<pre class="language-sql"><code>
db.collection.deleteOne({ name: "John Doe" });
</code></pre>
</li>
</ol>
<!-------Reference ------->
<section id="reference">
<h2>References</h2>
<ul>
<li><a href="https://github.com/mongodb/mongo" target="_blank">MongoDB github</a></li>
<li><a href="https://www.mongodb.com/docs/manual/reference/method/" target="_blank">MongoDB documentation</a>.</li>
</ul>
</section>
<hr>
<div style="background-color: #f0f0f0; padding: 15px; border-radius: 5px;">
<h3>Some other interesting things to know:</h3>
<ul style="list-style-type: disc; margin-left: 30px;">
<li>Visit my website on <a href="sql-project.html">For Data, Big Data, Data-modeling, Datawarehouse, SQL, cloud-compute.</a></li>
<li>Visit my website on <a href="Data-engineering.html">Data engineering</a></li>
</ul>
</div>
<p></p>
<div class="navigation">
<a href="index.html#portfolio" class="clickable-box">
<span class="arrow-left">Portfolio section</span>
</a>
<a href="Data-engineering.html" class="clickable-box">
<span class="arrow-right">Content</span>
</a>
</div>
</div>
</div>
</section><!-- End Portfolio Details Section -->
</main><!-- End #main --
<!-- ======= Footer ======= -->
<footer id="footer">
<div class="container">
<div class="copyright">
© Copyright <strong><span>Arun</span></strong>
</div>
</div>
</footer><!-- End Footer -->
<a href="#" class="back-to-top d-flex align-items-center justify-content-center"><i class="bi bi-arrow-up-short"></i></a>
<!-- Vendor JS Files -->
<script src="assets/vendor/purecounter/purecounter_vanilla.js"></script>
<script src="assets/vendor/aos/aos.js"></script>
<script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="assets/vendor/glightbox/js/glightbox.min.js"></script>
<script src="assets/vendor/isotope-layout/isotope.pkgd.min.js"></script>
<script src="assets/vendor/swiper/swiper-bundle.min.js"></script>
<script src="assets/vendor/typed.js/typed.umd.js"></script>
<script src="assets/vendor/waypoints/noframework.waypoints.js"></script>
<script src="assets/vendor/php-email-form/validate.js"></script>
<!-- Template Main JS File -->
<script src="assets/js/main.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
hljs.initHighlightingOnLoad();
});
</script>
</body>
</html>