MongoDB Workshop.pptx computer science and engineering

sanjay21042 17 views 46 slides Oct 07, 2024
Slide 1
Slide 1 of 46
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46

About This Presentation

Cse


Slide Content

MongoDB Workshop

Objective Introduction to Database Introduction to MongoDB Installation and Setups Basic CRUD Operations Entity Relationship Model Aggregations

Data & Database Data - Collection of Information D atabase -  Organized collection of structured information, or data. Types of Database: Relational databases  Relational database are organized as a data of tables with columns and rows. NoSQL databases A NoSQL database store the data in Document/Json oriented information

MongoDB MongoDB is an open-source document-oriented database that is designed to store a large scale of data and also allows you to work with that data very efficiently. Dwight merriman , Eliot Horowitz – 2007 -10Gen The MongoDB database is developed and managed by MongoDB.Inc under SSPL(Server Side Public License) and initially released in February 2009. It also provides official driver support for all the popular languages like C, C++, C#, and .Net , Go, Java, Node.js, Perl, PHP, Python, Motor, Ruby, Scala, Swift.

MongoDB Features of MongoDB : Schema-less Database Document Oriented Aggregation Scalability High Performance

MongoDB Advantages of MongoDB :   It is a schema-less NoSQL database. You need not to design the schema of the database when you are working with MongoDB. It does not support join operation. It contains heterogeneous data. It provides high performance, scalability. It is a document oriented database and the data is stored in J SON documents. It is easily integrated with Big Data Hadoop Disadvantages of MongoDB :   It uses high memory for data storage. You are not allowed to store more than 16MB data in the documents. The nesting of data in BSON is also limited you are not allowed to nest data more than 100 levels.

Installation and Setup MongoDB Community service - https://www.mongodb.com/try/download/community MongoDB Shell - https://www.mongodb.com/try/download/shell MongoDB Compass - https://www.mongodb.com/try/download/compass MongoDB Atlas - https://www.mongodb.com/cloud/atlas/register

Basic CRUD Operations CRUD – Create, Read, Update, Delete Shell connect with Compass: MongoDB Shell => Connection name

Basic CRUD Operations Create Database: use <Database Name> use Students Create Collection: db.createCollection (<collection name>) db.createCollection (“ studentsDetails ”)

Basic CRUD Operations Insert the Collection Data: db.<collection name>. insertOne (<inserting one data>) db.<collection name>. insertMany (<inserting Multiple data>) db.studentDetails.insertMany ([your data])

Basic CRUD Operations Read the Collection: db.<collection name>.find() db.<collection name>.find().pretty() db.studentsDetails.find ();

Basic CRUD Operations Update the Collection: db.<collection name>.update(<Identify data>,<Change content>) db.<collection name>. updateOne (<Identify data>,<Change content>) db.<collection name>. updateMany (<Identify data>,<Change content>) db.studentsDetails.updateOne ({student_id:2},{$set:{first_name:” steve ”}})

Basic CRUD Operations Delete the Collection : db.<collection name>.remove(<Identify data>) db.studentsDetails.remove ({date_of_birth:”1998-05-15”});

Basic CRUD Operations Sort the Data: Data's arranged in ascending order or descending order db.<collection name>.find().sort({field:1 or -1}) db.studentsDetails.find ().sort({ student_id :-1}) db.Collection_Name.sort({field_name:1 or -1})

Basic CRUD Operations Limit() method: To limit the records in MongoDB, you need to use limit() method. db.<collection name>.find(<query>).limit(<number>) db.studentsDetails.find ().limit(1); db.Collection_Name.sort({field_name:1 or -1})

Basic CRUD Operations Skip() method: the skip() method will skip the first n document from the query result, you just need to pass the number of records/documents to be skipped. db.< collectionname >.find(<query>).skip(<number>) ; db.studentsDetails . find().skip(1); db.Collection_Name.sort({field_name:1 or -1})

Basic CRUD Operations Distinct() method: the distinct() method finds the distinct values for a given field across a single collection and returns the results in an array. db.<collection name>.distinct(<field>) db.studentsDetails.distinct (“ first_name ”) db.Collection_Name.sort({field_name:1 or -1})

MongoDB Projection What is MongoDB Projection? MongoDB Projection is a special feature allowing you to select only the necessary data rather than selecting the whole set of data from the document. For Example, If a Document contains 10 fields and only 5 fields are to be shown the same can be achieved using the Projections. This will enable us to: Project concise yet transparent data Filter data without impacting the overall database performance. db.<Collection name>.find({},{field1 : value, fiel2d : value, …}) db.studentsDetails.find ({}.{student_id:1,first_name_1,_id:0})

Update Modifiers $ inc operator : This operator is used to increment the value of the field by the specified amount. db.<collection name>.update({<Identifier>},{$ inc : { <field : value>}}); db.employees.update ({eid:3},{$ inc :{salary:500}})

Update Modifiers $ mul operator : This operator is used to multiply the value of the field by the specified amount. db.<collection name>.update({<Identifier>},{$ mul : { <field : value>}}); db.employees.update ({eid:3},{$ mul :{salary:2}})

Update Modifiers $ rename operator : This operator is used to rename a field. db.<collection name>.update({<Identifier>},{$rename: { <field : updateField >}}); db.employees.update ({eid:3},{$rename:{ salary:”Amount ”}})

Update Modifiers $set operator : The set operator changes the value of a field with the specified value. db.<collection name>.update({<identifier>},{$set:{<field : value>}}); db.employees.update ({eid:1},{$set:{salary:70000}})

Update Modifiers $unset operator : It removes a specified field. db.<collection name>.update({<identifier>},{$unset:{<field : “”>}}); db.employees.update ({eid:1},{$unset:{address:” ”}})

Update Modifiers $ push operator : It appends a specified value to an array db.<collection name>.update({<identifier>},{$push:{<field : value>}}); db.employees.update ({eid:1},{$push:{ email:”[email protected] ”}})

Update Modifiers $ each operator : It is used with the $addToSet operator and the $push operator. It is used with the addToSet operator to add multiple values to an array if the value does not exist in the field. db.<collection name>.update({<identifier>},{$push:{<field>:{$each:[value1,value2, ..]}}}) db.employees.update ({eid:1},{$push:{phone:{$each:[“9887656379”,”34709484-1-0”]}}}}) ;

Update Modifiers $ addToSet operator : It adds a value to an array unless the value is already present, db.<collection name>.update({<identifier>},{$addToSet:{<field : value>, …}}); db.employees.update ({eid:3},{$ addToSet :{phone:”7788237782”}})

Update Modifiers $ pull operator : This operator is used to remove all the instances of the value or the value that matches the specified condition from the existing array. db.<collection name>.update({<identifier>},{$pull:{<field>: value}, …}) db.employees.update ({eid:3},{$pull:{ course:”History ”}})

Update Modifiers $ pullAll operator : This operator is used to remove all instances of the specified values from an existing array. db.<collection name>.update({<identifier>},{$ pullAll :{<field>: [value1, value2,…]}, …}); db.employees.update ({eid:2},{$ pullAll :{phone:[”897464893”,”7178701824”]}})

Condition operators $ in operator : it is used to select documents in which the field's value equals any of the given values in the array db.<collection name>.find({field : {$in : [value1, value2,..]}}) db.employees.find ({dept:{$in:[“HR”]}})

Condition operators $ gte operator :  is used to select documents where the value of the field is greater than or equal to (i.e. >=) the given value db.<collection name>.find({field : {$ gte : value}}) db.employees.find ({salary:{$gte:80000}})

Condition operators $ lte operator : It is used to select documents where the value of the field is less than or equal to (i.e. <=) the given value.  db.<collection name>.find({field : {$ lte : value}}) db.employees.find ({salary:{$lte:80000}})

Entity Relationship Model ER model stands for an Entity-Relationship model. It is a high-level data model. This model is used to define the data elements and relationship for a specified system. For example,  Suppose we design a school database. In this database, the student will be an entity with attributes like address, name, id, age, etc. The address can be another entity with attributes like city, street name, pin code, etc and there will be a relationship between them. Entity : An entity may be any object, class, person or place. Weak entity - An entity that depends on another entity called a weak entity.

Entity Relationship Model Attribute : The attribute is used to describe the property of an entity. For example,  id, age, contact number, name, etc. can be attributes of a student. Key Attribute - The key attribute is used to represent the main characteristics of an entity. 

Entity Relationship Model Composite Attribute - An attribute that composed of many other attributes is known as a composite attribute. Multivalued Attribute - An attribute can have more than one value. For example,  a student can have more than one phone number.

Entity Relationship Model Derived Attribute - An attribute that can be derived from other attribute is known as a derived attribute For example,  A person's age changes over time and can be derived from another attribute like Date of birth.

Entity Relationship Model  Relationship : A relationship is used to describe the relation between entities. One-to-One Relationship - When only one instance of an entity is associated with the relationship. For example,  A student can applied to one Aadhar card student Aadhar Applied to 1 1

Entity Relationship Model One-to-many relationship : When only one instance of the entity on the left, and more than one instance of an entity on the right associates with the relationship then this is known as a one-to-many relationship. For example,  Scientist can invent many inventions, but the invention is done by the only specific scientist.

Entity Relationship Model Many-to-one relationship : When more than one instance of the entity on the left, and only one instance of an entity on the right associates with the relationship then it is known as a many-to-one relationship. For example,  Student enrolls for only one course, but a course can have many students.

Entity Relationship Model When more than one instance of the entity on the left, and more than one instance of an entity on the right associates with the relationship then it is known as a many-to-many relationship. For example,  Many Employee can work in many duty shifts. Employee Duty shifts Works in M M

Aggregation in MongoDB Aggregation operations process the data records/documents and return computed results. It collects values from various documents and groups them together and then performs different types of operations on that grouped data like sum, average, minimum, maximum, etc to return a computed result. MongoDB provides three ways to perform aggregation: Aggregation pipeline Map-reduce function Single-purpose aggregation

Aggregation in MongoDB Aggregation pipeline : Stages : $match - stage – filters those documents we need to work with, those that fit our needs $group - stage – does the aggregation job $sort - stage – sorts the resulting documents the way we require (ascending or descending) db.<collection Name>.aggregate(stages, Expression, operators) ;

Aggregation in MongoDB Map Reduce M ap reduce  is used for aggregating results for the large volume of data. Map reduce has two main functions one is a  map  that groups all the documents and the second one is the  reduce  which performs operation on the grouped data. db.<collection name>. mapReduce ( mappingFunction , reduceFunction , { out:”Result ”})

Aggregation in MongoDB Single Purpose Aggregation It is used when we need simple access to document like counting the number of documents or for finding all distinct values in a document. db.<collection name>.distinct(<field>) db.<collection name>.count()

Q&A What is different between Skip and limit method? Does MongoDB support primary-key, foreign-key relationship? In which language MongoDB is written? What is CRUD in MongoDB? What is the Mongo Shell? Types of attributes in ER Model What is relationship in ER Model

Thank You.
Tags