0% found this document useful (0 votes)
3 views

UE20MC505B_Unit1_Lecturenotes

Uploaded by

sreenu_pes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

UE20MC505B_Unit1_Lecturenotes

Uploaded by

sreenu_pes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Mongo DB and Basic CRUD Operations | Dr.

Lekha A

MongoDB
Unit 1

History

The development of MongoDB began in 2007 by a New York based organization named 10gen
which is now known as MongoDB Inc.
It was initially developed as a PAAS (Platform As A Service).
Later in 2009, it was introduced in the market as an open source database server that was
maintained and supported by MongoDB Inc.
The first ready production of MongoDB has been considered from version 1.4 which was released
in March 2010.
Latest version is 3.6.

Introduction

MongoDB is a document-oriented database.


It is a key feature of MongoDB.
It offers a document-oriented storage.
MongoDB stores data in flexible, JSON-like documents,
meaning fields can vary from document to document and data
structure can be changed over time
The document model maps to the objects in your application
code, making data easy to work with.
Ad hoc queries, indexing, and real time aggregation provide powerful ways to access and analyze
your data
MongoDB is a distributed database at its core, so high availability, horizontal scaling, and
geographic distribution are built in and easy to use
MongoDB is free and open-source, published under the GNU Affero General Public License.
MongoDB – Advantages

Fast, Iterative Development


₪ Scope creep and changing business requirements no longer stand between you and successful
project delivery.
₪ A flexible data model coupled with dynamic schema, and idiomatic drivers, with powerful GUI
and command line tools make it fast for developers to build and evolve applications.
₪ Automated provisioning and management enable continuous integration and delivery for
highly productive operations.
Flexible Data Model
₪ MongoDB stores data in flexible, JSON-like documents, making it easy for you to persist and
combine data of any structure.

pg. 1
Mongo DB and Basic CRUD Operations | Dr. Lekha A

₪ The document model maps to the objects in your application code, making data easy to work
with, without giving up schema governance controls, data access, complex aggregations, and
rich indexing functionality.
₪ The schema can be dynamically modified without downtime.
Distributed Data Platform
₪ MongoDB can be run within and across geographically distributed data centers and cloud
regions, providing new levels of availability and scalability.
₪ MongoDB scales elastically with no downtime, and without changing the application.
₪ As performance and availability goals evolve, MongoDB lets the developer adapt flexibly,
across data centers, with tunable consistency.
Integrated Feature Set
₪ Analytics and data visualization, text and geospatial search, graph processing, event-driven
streaming data pipelines, in-memory performance and global replication allow to deliver a
wide variety of real-time applications on one technology, reliably and securely.
₪ RDBMS systems require additional, complex technologies demanding separate integration
overhead and expense to do this well.
Lower Total Cost of Ownership
₪ Application development teams are 2x and more productive when they use MongoDB.
₪ The fully managed Atlas cloud service means operations team are as well.
₪ MongoDB runs on commodity hardware, dramatically lowering costs.
₪ MongoDB offers on-demand, pay-as-you-go pricing and affordable annual subscriptions,
including 24x7x365 global support.
₪ Applications can be one tenth the cost to deliver compared to using a relational database.

Reasons for moving away from RDBMS

Expressive query language & secondary Indexes


₪ Users should be able to access and manipulate their data in sophisticated ways to support
both operational and analytical applications.
₪ Indexes play a critical role in providing efficient access to data, supported natively by the
database rather than maintained in application code.
Strong consistency
₪ Applications should be able to immediately read what has been written to the database.
₪ It is much more complex to build applications around an eventually consistent model,
imposing significant work on the developer, even for the most sophisticated engineering
teams.
Enterprise Management and Integrations
₪ Databases are just one piece of application infrastructure, and need to fit seamlessly into the
enterprise IT stack.

pg. 2
Mongo DB and Basic CRUD Operations | Dr. Lekha A

₪ Organizations need a database that can be secured, monitored, automated, and integrated
with their existing technology infrastructure, processes, and staff, including operations teams,
DBAs, and data analysts.
Features of architecture

MongoDB’s flexible document data model presents a superset of other database models.
₪ It allows data to be represented as simple key-value pairs and flat, table-like structures,
through to rich documents and objects with deeply nested arrays and sub-documents.
With an expressive query language, documents can be queried in many ways.
₪ Simple lookups
₪ Sophisticated processing pipelines for data analytics and transformations, through to faceted
search, JOINs and graph traversals.
With a flexible storage architecture, application owners can deploy storage engines optimized for
different workload and operational requirements.
Data in MongoDB

MongoDB stores data in a binary representation called BSON (Binary JSON).


The BSON encoding extends the popular JSON (JavaScript Object Notation) representation to
include additional types such as int, long, date, floating point, and decimal128.
BSON documents contain one or more fields, and each field contains a value of a specific data type,
including arrays, binary data and sub-documents.
MongoDB BSON documents are closely aligned to the structure of objects in the programming
language.
₪ This makes it simpler and faster for developers to model how data in the application will map to
data stored in the database.
MongoDB documents tend to have all data for a given record in a single document, whereas in a
relational database information for a given record is usually spread across many tables.
Example
₪ Consider the data model for a blogging application. In a relational database, the data model
would comprise multiple tables such as Categories, Tags, Users, Comments and Articles.
₪ In MongoDB the data could be modeled as two collections, one for users, and the other for
articles.
 In each blog document there might be multiple comments, multiple tags, and multiple
categories, each expressed as an embedded array.

pg. 3
Mongo DB and Basic CRUD Operations | Dr. Lekha A

Data in MongoDB is more localized, which dramatically reduces the need to JOIN separate tables.
The result is dramatically higher performance and scalability across commodity hardware as a
single read to the database can retrieve the entire document.
Unlike many NoSQL databases, users don’t need to give up JOINs entirely.
₪ For additional flexibility, MongoDB provides the ability to perform equi and non-equi JOINs that
combine data from multiple collections, typically when executing analytical queries against live,
operational data.
Collection

Collections are simply groups of documents.


₪ Since documents exist independently, they can have different fields.
It is referred to as dynamic schema.
Field Names

Field names are strings.


Documents have the following restrictions on field names:
₪ The field name _id is reserved for use as a primary key;
₪ Its value must be unique in the collection, is immutable, and may be of any type other than an
array.
₪ The field names cannot start with the dollar sign ($) character.
₪ The field names cannot contain the dot (.) character.
₪ The field names cannot contain the null character.
ObjectID

ObjectId is a 12-byte BSON type, constructed using:


₪ a 4-byte value representing the seconds since the Unix epoch,
₪ a 3-byte machine identifier,
₪ a 2-byte process id, and
₪ a 3-byte counter, starting with a random value
Example
pg. 4
Mongo DB and Basic CRUD Operations | Dr. Lekha A

₪ "_id" : ObjectId("5699348d350ba4219fca39a1")
₪ "_id" : ObjectId("56993492350ba4219fca39a2")
Document

Documents vs. Collections

Commands

Connect to mongod
₪ mongo
Clear the screen
₪ cls
Create a new database if it does not exist or returns an existing database
₪ use <database_name>
₪ output
 switched to db database_name
After starting the mongo shell, session will use the test database by default.
Check the currently selected database_name

pg. 5
Mongo DB and Basic CRUD Operations | Dr. Lekha A

₪ db
₪ output
 database_name
check the databases list
₪ show dbs
₪ Output
 table size
To display a database atleast one document should be inserted into it
Drop an existing databse
₪ db.dropDatabase()
₪ Output
 if not drop the default test database
Create a collection
₪ db.createCollection(name, options)
 name is name of collection to be created.
 Options is a document and is used to specify configuration of collection.
 Options can be
 Capped
 It is boolean and optional
 If true, enables a capped collection.
 Capped collection is a fixed size collection that automatically overwrites its oldest entries
when it reaches its maximum size.
 If we specify true, we need to specify size parameter also.
 db.createCollection(“MCA", {capped : true, size : 5242880, max : 5000 } )
 autoIndexID
 It is boolean and optional
 If true, automatically create index on _id fields.
 Default value is false.
 size number
 It is optional
 Specifies a maximum size in bytes for a capped collection.
 If capped is true, then we need to specify this field also.
 max number
 It is optional
 Specifies the maximum number of documents allowed in the capped collection.

CRUD Commands – Create or Insert

Inserts a document or documents into a collection


₪ db.<collection_name>.insert( <document> )
₪ Output

pg. 6
Mongo DB and Basic CRUD Operations | Dr. Lekha A

 WriteResult({“nInserted”: 1})
₪ During insert mongod will create the _id field and assign it a unique ObjectId value
Insert a document specifying an _id field
₪ db. <collection_name>.insert({_id: num, filed_name: value})
 The value of _id must be unique within the collection to avoid duplicate key error.
Insert multiple documents
₪ db. <collection_name>.insert({<Document1>}, {<Document2>,…})
Unordered Insert
₪ db.<collection_name>.insert(
[ {<Document1>}, {<Document2>},….],
{ordered: false})
 With unordered inserts, if an error occurs during an insert of one of the documents, MongoDB
continues to insert the remaining documents in the array.
Insert Single Document
₪ db.<collection_name>.insertOne({<Document1>})
Insert a document specifying an _id field
₪ db. <collection_name>.insertOne({_id: num, field_name: value})
 The value of _id must be unique within the collection to avoid duplicate key error.
Insert Multiple Documents with Unordered Insert
₪ db.<collection_name>.insertMany(
[ {<document 1>}, {<document 2>}, ...],
{ordered: <boolean>})
Insert a document specifying an _id field
₪ db. <collection_name>.insertMany(
{_id: num, field_name: value},
{_id: num, field_name: value} )
 The value of _id must be unique within the collection to avoid duplicate key error.
Update an existing document or insert a new document, depending on its document parameter.
₪ db.<collection_name>.save( {<document>})
₪ db.<collection_name>.save( {<_id: num, field_name: value, ..>})
Modifies an existing document or documents in a collection.
₪ The method can modify specific fields of an existing document or documents or replace an
existing document entirely, depending on the update parameter.
 db.<collection_name>.update(
{field_name: existing_value },
{field_name: existing_value, new_field: new_value, existing_field : new_value },
{upsert: true})

CRUD Commands – Read

Selects documents in a collection or view and returns a pointer to the selected documents.

pg. 7
Mongo DB and Basic CRUD Operations | Dr. Lekha A

₪ db.<collection_name>.find(query, projection)
 The query parameter is optional. Specifies selection filter using query operators. To return all
documents in a collection, omit this parameter or pass an empty document ({}).
 The projection parameter determines which fields are returned in the matching documents.

The find() method with no parameters returns all documents from a collection and returns all fields
for the documents.
₪ db.<collection_name>.find()

Display only id
₪ db.<collection_name>.find({},{_id:1})

Display two fields (id and other)


₪ db.<collection_name>.find({},{_id:1,<field>:1})

Display all the fields except id


₪ db.<collection_name>.find({},{_id:0})

Display fields except id and dept

pg. 8
Mongo DB and Basic CRUD Operations | Dr. Lekha A

Display only one field (other than id)


₪ db.<collection_name>.find({},{_id:0, <field>: 1})

Returns one document that satisfies the specified query criteria on the collection.
₪ db.<collection_name>.findOne(query, projection)
 If multiple documents satisfy the query, this method returns the first document according to
the natural order which reflects the order of documents on the disk.
 In capped collections, natural order is the same as insertion order. If no document satisfies
the query, the method returns null.
Returns a single document without any query.
₪ db.<collection_name>.findOne()

Returns the first limit of documents.


₪ db.<collection_name>.find().limit(<number>)

pg. 9
Mongo DB and Basic CRUD Operations | Dr. Lekha A

Returns a limit of documents that satisfy a query.


₪ db.<collection_name>.find(query, options).limit(<number>)
Skipping the first set of documents.
₪ db.<collection_name>.find().skip(<number>)

To display last records.


₪ db.<collection>.find().skip(db.<collection>.count() – N)

To display in reverse order


₪ db.collection.find().sort({ $natural: -1 }).limit(N)

CRUD Commands – Update

pg. 10
Mongo DB and Basic CRUD Operations | Dr. Lekha A

Modifies an existing document or documents in a collection.


₪ The method can modify specific fields of an existing document or documents or replace an
existing document entirely, depending on the update parameter.
₪ db.<collection_name>.update( { field_name: existing_value },
{field_name: existing_value, new_field: new_value, existing_field : new_value },
{upsert: true})
Replace a single document within the collection based on the filter.
₪ db.<collection_name>.replaceOne(<filter>, <replacement>)
 In a capped collection if a replacement operation changes the document size, the operation
will fail.
CRUD Commands – Update Operators

Name Description

 Sets the value of a field to current date, either as a Date or a Timestamp.


 {$currentDate: {<field1>: <typeSpecification1>, ...}}
 <typeSpecification> can be either:
 a boolean true to set the field value to the current date as a Date, or
 a document { $type: "timestamp" } or { $type: "date" } which explicitly specifies
the type.
 The operator is case-sensitive and accepts only the lowercase "timestamp" or the
$currentDate lowercase "date".
 Update the lastModified field to the current date, the "cancellation.date" field to the
current timestamp as well as update the status field to "D" and the
"cancellation.reason" to "user request".

 Only updates the field if the specified value is less than the existing field value.
 {$min: {<field1>: <value1>, ...}}
 If the field does not exist, the $min operator sets the field to the specified value.
$min
 For comparisons between values of different types, such as a number and a
null, $min uses the BSON comparison order.
 Compute the minimum amount and minimum quantity for each grouping.

pg. 11
Mongo DB and Basic CRUD Operations | Dr. Lekha A

 Only updates the field if the specified value is greater than the existing field value.
 {$max: {<field1>: <value1>, ...}}
 The $max operator updates the value of the field to a specified value if the specified
value is greater than the current value of the field.
 The $max operator can compare values of different types, using the BSON comparison
order.
$max  Compute the maximum amount and maximum quantity for each grouping.

 Multiplies the value of the field by the specified amount.


 {$mul: {field: <number>}}
 If the field does not exist in a document, $mul creates the field and sets the value to
zero of the same numeric type as the multiplier.
$mul

 Sets the value of a field in a document.


 {$set: {<field1>: <value1>, ...}}
 If the field does not exist, $set will add a new field with the specified value, provided
$set
that the new field does not violate a type constraint.
 For the document matching the criteria _id equal to 100, update the value of the
quantity field, details field, and the tags field.

pg. 12
Mongo DB and Basic CRUD Operations | Dr. Lekha A

 Sets the value of a field if an update results in an insert of a document. Has no effect
on update operations that modify existing documents.
 {$setOnInsert: {<field1>: <value1>, ...}}

$setOnInsert

 Removes the specified field from a document.


 {$unset: {<field1>: "", ...}}
 If the field does not exist, then $unset does nothing (i.e. no operation).
 Delete the purqty field from the document for _id : 1
$unset

CRUD Commands – Update Operators on Arrays

Name Description

pg. 13
Mongo DB and Basic CRUD Operations | Dr. Lekha A

 The positional $ operator identifies an element in an array to update without explicitly


specifying the position of the element in the array.
 {"<array>.$" : value }
 Update the value of the std field in the first embedded document that has grade field
with a value less than or equal to 90 and a mean field with a value greater than 80

 The all positional operator $[] indicates that the update operator should modify all
elements in the specified array field.
 {<update operator>: {"<array>.$[]" : value } }
 To increment all elements in the grades array by 10 for all documents in the collection

$[]

pg. 14
Mongo DB and Basic CRUD Operations | Dr. Lekha A

 The filtered positional operator $[<identifier>] identifies the array elements that
match the arrayFilters conditions for an update operation.
 {<update operator>:
{"<array>.$[<identifier>]" : value } },
{arrayFilters: [{<identifier>: <condition>}}]}
 To increment all elements in the grades array by 10 for all documents in the collection

$[<identifier>]

 The $addToSet operator adds a value to an array unless the value is already present,
in which case $addToSet does nothing to that array.
 {$addToSet: {<field1>: <value1>, ...}}
 Add the element "accessories" to the tags array since "accessories" does not exist in
the array

$addToSet

pg. 15
Mongo DB and Basic CRUD Operations | Dr. Lekha A

 The $pop operator removes the first or last element of an array.


 Pass $pop a value of -1 to remove the first element of an array and 1 to remove the
last element in an array.
 {$pop: {<field>: <-1 | 1>, ...}} {$addToSet: {<field1>: <value1>, ...}}
 Remove the first element in the grades array

$pop

 The $push operator appends a specified value to an array.


 {$push: {<field1>: <value1>, ...}}
 Appends 89 to the grades array

$push

pg. 16
Mongo DB and Basic CRUD Operations | Dr. Lekha A

 The $pull operator removes from an existing array all instances of a value or values
that match a specified condition.
 {$pull: {<field1>: <value|condition>, <field2>: <value|condition>, ...}}
 Remove the arrays that have elements lesser than 95

$pull

 The $pullAll operator removes all instances of the specified values from an existing
array.
 Unlike the $pull operator that removes elements by specifying a query, $pullAll
removes elements that match the listed values.
 {$pullAll: {<field1>: [ <value1>, <value2> ... ], ... }}
 Remove the instances of all value 75, 80 from the grades array
$pullAll

pg. 17
Mongo DB and Basic CRUD Operations | Dr. Lekha A

CRUD Commands – Update Modifiers

Name Description

 The $each modifier is available for use with the $addToSet operator and the $push
operator.
 Use with the $addToSet operator to add multiple values to an array <field> if the values
do not exist in the <field>.
 {$addToSet: {<field>: {$each: [ <value1>, <value2> ...]}}}
 Use with the $push operator to append multiple values to an array <field>.
 {$push: {<field>: {$each: [ <value1>, <value2> ...]}}}
 Append each element of [90, 92, 85] to the scores array for the document where
the _id field is 1

$each

 Add multiple elements to the tags array

pg. 18
Mongo DB and Basic CRUD Operations | Dr. Lekha A

 The $position modifier specifies the location in the array at which the $push operator inserts
elements.
 Without the $position modifier, the $push operator inserts elements to the end of the array.
 To use the $position modifier, it must appear with the $each modifier.

$position

 Update the grades field to add the elements 50, 60 and 70 to the beginning of the array

 Update the grades field to add the elements 90 and 80 at the array index of 2

pg. 19
Mongo DB and Basic CRUD Operations | Dr. Lekha A

 The $slice modifier limits the number of array elements during a $push operation.
 To use the $slice modifier, it must appear with the $each modifier.
 Can pass an empty array [] to the $each modifier such that only the $slice modifier has an
effect.

 Add new elements to the grades array and use the $slice modifier to trim the array to the
last five elements.

$slice

 Add new elements to the grades array and use the $slice modifier to trim the array from
first three elements.

 The $sort modifier orders the elements of an array during a $push operation.
$sort
 To use the $sort modifier, it must appear with the $each modifier.

pg. 20
Mongo DB and Basic CRUD Operations | Dr. Lekha A

 Push the grades in sorted order and sort the grades in descending order.

CRUD Commands – Delete

Remove all documents from a collection.


₪ db.<collection_name>.remove({})
Remove all documents that match a condition
₪ db.<collection_name>.remove( { field_name: { query } } )
Remove a single document that matches a condition
₪ Call the remove method with the query criteria and the justOne parameter set to true or 1.
 Delete all grades arrays that have at least one value that is greater than 95

Removes a single document from a collection

pg. 21
Mongo DB and Basic CRUD Operations | Dr. Lekha A

₪ db.<collection_name>.deleteOne( <filter>)
 Use a field that is part of a unique index such as _id for precise deletions.
Removes all documents that match the filter from a collection.
₪ db.<collection_name>.deleteMany(<filter>)
 Delete the row that contains id=1

 Delete all rows that contain camera in its tags.

Delete a single document based on the filter and sort criteria, returning the deleted document.
₪ db.<collection_name>.findOneAndDelete(filter, options)
 Find the first document where name is ‘M. MMM’ and deletes it

CRUD Commands – Modify

Modify and return a single document.


₪ db.<collection_name>.findAndModify(document)
 Increment the field ‘points’ based on a query.

pg. 22
Mongo DB and Basic CRUD Operations | Dr. Lekha A

MongoDB Commands

Perform multiple write operations with controls for order of execution.


₪ db.collection.bulkWrite([ <operation 1>, <operation 2>, ... ])
 By default, operations are executed in order.

Variations in Insert

Create documents using JavaScript operations


₪ d1={_id: 1, name: "Lekha", city: "Bangalore", age: 18, marks: [90, 90, 90]}
₪ d2={_id: 2, name: "Manish", city: "Mangalore", age: 18, marks: [99, 90, 99]}
₪ db.Semfour.insert(d1)
₪ db.Semfour.insert(d2)
₪ db.Semfour.find()
 { "_id" : 1, "name" : "Lekha", "city" : "Bangalore", "age" : 18, "marks" : [ 90, 90, 90 ] }
 { "_id" : 2, "name" : "Manish", "city" : "Mangalore", "age" : 18, "marks" : [ 99, 90, 99 ] }
Insert an array of documents
₪ var MyDocument=[
{_id:1, name:"Lekha", age:18, city: "Bangalore", marks:[90, 90, 90]},
{_id:2, name:"Manish", age:18, city: "Mangalore", marks:[99, 90, 90]},
{_id:3, name:"Isha", age: 17, city: "Bangalore", marks: [99,98,97]}]
₪ db.Semfour.insert(MyDocument)
₪ Output
 BulkWriteResult({"writeErrors" : [], "writeConcernErrors" : [], "nInserted" : 3, “nUpserted" :
0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [] })
Embedded Data Model

• Also called as denormalized data model.


• Embedded data models allow applications to store related pieces of information in the same
database record.
• As a result, applications may need to issue fewer queries and updates to complete common
operations.

pg. 23
Mongo DB and Basic CRUD Operations | Dr. Lekha A

Normalized Data Models

• Normalized data models describe relationships using references between documents.


• References provides more flexibility than embedding.
• However, client-side applications must issue follow-up queries to resolve the references.
• In other words, normalized data models can require more round trips to the server.

pg. 24

You might also like