MongoDB Workshop.pptx computer science and engineering
sanjay21042
17 views
46 slides
Oct 07, 2024
Slide 1 of 46
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
About This Presentation
Cse
Size: 1.2 MB
Language: en
Added: Oct 07, 2024
Slides: 46 pages
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 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