0% found this document useful (0 votes)
238 views53 pages

Unit 5 - Chapter 2 - Introduction To MongoDB

MongoDB is a popular open-source document-oriented NoSQL database written in C++. It stores data in flexible JSON-like documents, allowing for dynamic schemas. MongoDB is commonly used for large volumes of data and real-time applications due to its horizontal scalability and high performance. Some key companies that use MongoDB include IBM, Twitter, T-Mobile, and Sony.

Uploaded by

Shamanth Edge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
238 views53 pages

Unit 5 - Chapter 2 - Introduction To MongoDB

MongoDB is a popular open-source document-oriented NoSQL database written in C++. It stores data in flexible JSON-like documents, allowing for dynamic schemas. MongoDB is commonly used for large volumes of data and real-time applications due to its horizontal scalability and high performance. Some key companies that use MongoDB include IBM, Twitter, T-Mobile, and Sony.

Uploaded by

Shamanth Edge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

Introduction to MongoDB

• MongoDB is one of the most popular open-source NoSQL database


written in C++.
• As of February 2015, MongoDB was the fourth most popular
database management system. It was developed by a company
10gen which is now known as MongoDB Inc.
• MongoDB is a document-oriented database which stores data in JSON-
like documents with dynamic schema.
• It means you can store your records without worrying about the data structure
such as the number of fields or types of fields to store values.
• MongoDB documents are similar to JSON objects.
Version History
• MongoDB is a document-based database which is developed in the
C++ programming languages.
• The word Mongo is basically derived from Humongous.
• MongoDB was first developed by a New York-based organization
named 10gen in the year of 2007.
• Later 10gen changed the name and known as MongoDB Inc as of
today. At the beginning, MongoDB is basically developed as a PAAS
(Platform as a Service) database.
• But, in the year 2009, it was introduced as an open source database
as named MongoDB 1.0.
Who are Using MongoDB?
• In today’s IT industry, there are large number of companies who are using MongoDB as a
database service for the applications or data storage systems.
• As per the survey made by Siftery on MongoDB, there are around 4000+ company confirmed
that they are using MongoDB as Database.
• Some of the key names are :
• Castlight Health
• IBM
• Citrix
• Twitter
• T-Mobile
• Zendesk
• Sony
• BrightRoll
• Foursquare
• HTC
• InVision
• Intercom etc
Why and Where you should use Mongo DB?
• Since, MongoDB is a NoSQL database, so we need to understand when
and why we need to use this type of database in the real-life applications.
• MongoDB always preferred by the developers or project managers when
our main concern is the deal with large volume of data with a high
performance.
• There is no need to any type of administrative work for perform any type
of horizontal scaling in the MongoDB.
• MongoDB is good for the below types of situations :
• E-Commerce type of product-based applications
• Blog and Content Management systems
• High Speed logging, caching etc. in the Real time
• Need to maintain location wise Geospatial data
• For maintains data related to the Social and Networking types
• If application is a loosely coupled mechanism – means design may change at any
point of time.
Platform And Language Support
• Like other RDBMS systems, MongoDB also provides official support for a
large number of programming languages and frameworks.
• Mongo Drivers are available for the following popular languages and
Frameworks:
• C
• C++
• C# and .NET
• Java
• Node.js
• Perl
• PHP, PHP Libraries, Frameworks, and Tools.
• Python
• Ruby
• Mongoid (Ruby ODM)
Alternatives to MongoDB
• https://www.g2.com/products/mongodb/competitors/alternatives
• https://www.guru99.com/mongodb-alternative.html
Installing and working with
MongoDB
Kickstart
• Installing Mongo on Windows
• Download the relevant installable (check the following link for the
local server)
• https://www.mongodb.com/download-center/community
• Step by step guide to setup Mongo
• https://www.wdb24.com/how-to-install-mongodb-windows-10/
• Set the path using the environment variables setup
• Open command and type mongo
First screen
Alternatives to Mongo Links
• https://www.educba.com/mongodb-alternatives/
Terminologies and definitions
• MongoDB
• Is a cross-platform
• Document oriented database that provides
• Provides high performance, high availability, and easy scalability
• Works on concept of collection and document.
• Database
• Database is a physical container for collections. Each database gets its own set of files on the file
system.
• A single MongoDB server typically has multiple databases
• Collection
• Collection is a group of MongoDB documents
• It is the equivalent of an RDBMS table
• A collection exists within a single database
• Collections do not enforce a schema
• Documents within a collection can have different fields
• Typically, all documents in a collection are of similar or related purpose
• Document
• A document is a set of key-value pairs.
• Documents have dynamic schema.
• Dynamic schema means that documents in the same collection do not need to
have the same set of fields or structure, and common fields in a collection's
documents may hold different types of data.
Why Use MongoDB / features of MongoDB
• Document Oriented Storage 
• Data is stored in the form of JSON style documents.
• Index on any attribute
• Replication and high availability
• Auto-sharding
• Rich queries
• Fast in-place updates
• Professional support by MongoDB
Comparison of RDBMS with MongoDB

https://www.educba.com/what-is-mongodb/
Where to Use MongoDB?

• Big Data
• Content Management and Delivery
• Mobile and Social Infrastructure
• User Data Management
• Data Hub
The following table shows the relationship of RDBMS terminology with
MongoDB.
RDBMS MongoDB

Database Database

Table Collection

Tuple/Row Document

Column Field

Table Join Embedded Documents

Primary Key Primary Key (Default key _id provided


by mongodb itself)
Database Server and Client
Mysqld/Oracle mongod
mysql/sqlplus mongo
MongoDB - Advantages
• Any relational database has a typical schema design that shows number of tables
and the relationship between these tables. While in MongoDB, there is no
concept of relationship.
• Advantages of MongoDB over RDBMS
• Schema less − MongoDB is a document database in which one collection holds different
documents. Number of fields, content and size of the document can differ from one
document to another
• Structure of a single object is clear
• No complex joins
• Deep query-ability
• MongoDB supports dynamic queries on documents using a document-based query
language that's nearly as powerful as SQL
• Tuning
• Ease of scale-out − MongoDB is easy to scale
• Conversion/mapping of application objects to database objects not needed
• Uses internal memory for storing the (windowed) working set, enabling faster access of
data
Core of MongoDB
• It works on an extended version of JSON known as BSON (Binary
JSON), which is:
• Lightweight
• Traversable
• Efficient
• These drivers are responsible for sending and receiving data in BSON
format. It stores the data as a BSON Object. 
• Encoding to BSON and Decoding to BSON again happens very quickly,
and so it’s so efficient.
• Here are a few terms related to MongoDB, which is used while using it.
• Let’s get familiar with that:
Basic commands
• MongoDB use DATABASE_NAME is used to create database. The command will
create a new database if it doesn't exist, otherwise it will return the existing
database.
• Syntax use DATABASE_NAME
• Basic syntax of use DATABASE statement is as follows −
> use mydb
• To check your currently selected database, use the command
>db
• If you want to check your databases list, use the command 
>show dbs.
• Creating document in the database area
>db.movie.insert({“name”: “KHNH”})

• Note: In MongoDB default database is test. If you didn't create any database, then
collections will be stored in test database.
• Drop database
• To drop the database execute following command, this will drop the selected database
>db.dropDatabase()

• Create collection
To create the new collection execute the following commands
>db.createCollection(name)
Eg:db.createCollection(“Employee”)

• To check collections list

To get the list of collections created execute the following command


>show collections

• Drop collection

To drop the selected collection execute the following command


>db.COLLECTION_NAME.drop()
• Insert document in collection
>db.COLLECTION_NAME.insert(document)
Eg: db.Employee.insert({"name":"Rama"})
db.Employee.insert({"name":"Rama", "designation":"Manager"})
Get collection document

To get the list documents in collection execute the following command


>db.COLLECTION_NAME.find()
>db.Employee.find()
Introduction to MongoDB Shell
• To start the shell
Creating an blog post
Let us check this link…
• http://
guyharrison.squarespace.com/blog/2015/3/23/sakila-sample-sc
hema-in-mongodb.html
• https://beginnersbook.com/2017/09/mongodb-create-
collection/
• Create
• The insert function adds a document to a collection
• The previous example creates a post for blog – this JavaScript structure
contains the keys title, content and date and its corresponding values
• The created object is a valid MongoDB document and to save the values
we will use insert method
• > db.post.insert(post) // with this the post has been saved in the
database , check the created post with the object id
• Read using find() and findone()
• We can see the contents inserted by using the find() function
db.post.find()
find() and findOne()
- Both the methods can be used to query a collection
- Use findOne() if you want to see only one document from a collection
- Shell will automatically display upto 20 documents matching a find
• Update
• Permits modifications or updates
• Adding likes field to the post
post.likes=50
db.post.update({“title”:….}, post)
db.post.find()

• Delete
• remove permanently deletes documents from the database
>db.post.remove({“title”:…..})
Check back the collection
Basic commands
• MongoDB use DATABASE_NAME is used to create database. The command will
create a new database if it doesn't exist, otherwise it will return the existing
database.
• Syntax use DATABASE_NAME
• Basic syntax of use DATABASE statement is as follows −
> use mydb
• To check your currently selected database, use the command
>db
• If you want to check your databases list, use the command 
>show dbs.
• Creating document in the database area
>db.movie.insert({“name”: “KHNH”})

• Note: In MongoDB default database is test. If you didn't create any database, then
collections will be stored in test database.
• Drop database
• To drop the database execute following command, this will drop the selected database
>db.dropDatabase()

• Create collection
To create the new collection execute the following commands
>db.createCollection(name)
Eg:db.createCollection(“Employee”)
• https://docs.mongodb.com/manual/reference/method/db.createCollection/

• To check collections list

To get the list of collections created execute the following command


>show collections

• Drop collection

To drop the selected collection execute the following command


>db.COLLECTION_NAME.drop()
• Insert document in collection
>db.COLLECTION_NAME.insert(document)
Eg: db.Employee.insert({"name":"Rama"})
db.Employee.insert({"name":"Rama", "designation":"Manager"})
Get collection document

To get the list documents in collection execute the following command


>db.COLLECTION_NAME.find()
>db.Employee.find()
Data Types
• Basic Data Types
• Documents in MongoDB can be thought of as “JSON-like” in that they are
conceptually similar to objects in JavaScript.
• JSON is a simple representation of data: the specification can be described in about
one paragraph (their website proves it) and lists only six data types. This is a good
thing in many ways: it’s easy to understand, parse, and remember. On the other
hand, JSON’s expressive capabilities are limited because the only types are null,
boolean, numeric, string, array, and object.
• JSON has no date type, which makes working with dates even more annoying than it
usually is.
• There is a number type, but only one—there is no way to differentiate floats and
integers, never mind any distinction between 32-bit and 64- bit numbers.
• There is no way to represent other commonly used types, either, such as
regular expressions or functions.
• MongoDB adds support for a number of additional data types while keeping
JSON’s essential key/value pair nature.
• The list of the commonly supported types and how they are represented as part of
a document in the shell is given below:
• Null
• Boolean
• Number
• String
• Date
• Regular expression
• Array
• Embedded document
• Object Id
• Binary data
• Code
• null
• Null can be used to represent both a null value and a nonexistent field:
• {"x" : null}
• boolean
• There is a boolean type, which can be used for the values true and false:
• {"x" : true}
• number
• The shell defaults to using 64-bit floating point numbers. Thus, these numbers
look “normal” in the shell:
• {"x" : 3.14}
• For integers, use the NumberInt or NumberLong classes, which
represent 4-byte or 8-byte signed integers, respectively.
• {"x" : NumberInt("3")}
• {"x" : NumberLong("3")}
• string
• Any string of UTF-8 characters can be represented using the string type:
• {"x" : "foobar"}
• date
• Dates are stored as milliseconds since the epoch. The time zone is not stored:
• {"x" : new Date()}
• regular expression
• Queries can use regular expressions using JavaScript’s regular expression
syntax:
• {"x" : /foobar/i}
• array
• Sets or lists of values can be represented as arrays:
• {"x" : ["a", "b", "c"]}
• embedded document
• Documents can contain entire documents embedded as values in a parent
document:
• {"x" : {"foo" : "bar"}}
• object id
• An object id is a 12-byte ID for documents. See the section “_id and ObjectIds”
• {"x" : ObjectId()}
• There are also a few less common types that you may need, including:
• binary data
• Binary data is a string of arbitrary bytes. It cannot be manipulated from the shell.
• Binary data is the only way to save non-UTF-8 strings to the database.
• code
• Queries and documents can also contain arbitrary JavaScript code:
• {"x" : function() { /* ... */ }}
Dates
• In JavaScript, the Date class is used for MongoDB’s date type.
• When creating a new Date object, always call new Date(...), not just
Date(...).
• Calling the constructor as a function (that is, not including new)
returns a string representation of the date, not an actual Date
object.
• This is not MongoDB’s choice;
• it is how JavaScript works
• If you are not careful to always use the Date constructor, you can end up with a
mishmash of strings and dates.
• Strings do not match dates and vice versa, so this can cause problems with
removing, updating, querying…pretty much everything
• Dates in the shell are displayed using local time zone settings.
• However, dates in the database are just stored as milliseconds
since the epoch, so they have no time zone information associated
with them.
Arrays
• Arrays are values that can be interchangeably used for both ordered
operations (as though they were lists, stacks, or queues) and unordered
• In the following document, the key "things" has an array value:
Eg: {"things" : ["pie", 3.14]}
• Arrays
• can contain different data types as values
• can be any of the supported values for normal key/value pairs, even nested
arrays.
• One of the great things about arrays in documents is that MongoDB
“understands” their structure and knows how to reach inside of arrays to
perform operations on their contents.
• In the example, MongoDB can query for all documents where 3.14 is an
element of the "things" array.
• If this is a common query, it can even create an index on the "things" key
to improve the query’s speed.
Embedded Documents
• Documents can be used as the value for a key.
• This is called an embedded document. Embedded documents can be used to organize data in a more
natural way than just a flat structure of key/value pairs.
• For example, if we have a document representing a person and want to store his address, we can
nest this information in an embedded "address" document:
{
"name" : "John Doe",
"address" :
{
"street" : "123 Park Street",
"city" : "Anytown",
"state" : "NY"
}
}
• The value for the "address" key in the previous example is an embedded document with its own
key/value pairs for "street", "city", and "state".
• As with arrays, MongoDB “understands” the structure of embedded documents and is able to reach
inside them to build indexes, perform queries, or make updates.
_id and ObjectIds

• Every document stored in MongoDB must have an "_id" key.


• The "_id" key’s value can be any type, but it defaults to an
ObjectId.
• In a single collection,
• every document must have a unique value for "_id", which ensures that
every document in a collection can be uniquely identified.
• if you had two collections, each one could have a document where the
value for "_id" was 123.
• However, neither collection could contain more than one
document with an "_id" of 123.
ObjectIds

• ObjectId is the default type for "_id"


• The ObjectId class
• is designed to be lightweight
• easy to generate in a globally unique way across different
machines.
• ObjectIds as opposed to something more traditional, like an
auto incrementing primary key: it is difficult and time-
consuming to synchronize auto incrementing primary keys
across multiple servers.
• Because MongoDB was designed to be a distributed database,
it was important to be able to generate unique identifiers in a
sharded environment.
• ObjectIds use 12 bytes of storage, which gives them a string
representation that is 24 hexadecimal digits: 2 digits for each byte.
• This causes them to appear larger than they are, which makes some people
nervous.
• It’s important to note that even though an ObjectId is often represented
as a giant hexadecimal string, the string is actually twice as long as the
data being stored.
• If there is no "_id" key present when a document is inserted, one will be
automatically added to the inserted document.
• This can be handled by the MongoDB server but will generally be done by the driver on the client side.
• The decision to generate them on the client side reflects an overall philosophy of
MongoDB: work should be pushed out of the server and to the drivers whenever
possible.
• This philosophy reflects the fact that, even with scalable databases like
MongoDB, it is easier to scale out at the application layer than at the
database layer.
• Moving work to the client side reduces the burden requiring the database to scale.
Format of Object Id
• If you create multiple new ObjectIds in rapid succession, you can see
that only the last few digits change each time.
• In addition, a couple of digits in the middle of the ObjectId will change
(if you space the creations out by a couple of seconds).
• This is because of the manner in which ObjectIds are created.
• The 12 bytes of an ObjectId are generated as follows:
• The first four bytes of an ObjectId are a timestamp in seconds
since the epoch.
• This provides a couple of useful properties:
• The timestamp, when combined with the next five bytes provides
uniqueness at the granularity of a second.
• Because the timestamp comes first, it means that ObjectIds will sort in
roughly insertion order. This is not a strong guarantee but does have
some nice properties, such as making ObjectIds efficient to index.
• In these four bytes exists an implicit timestamp of when each document
was created. Most drivers expose a method for extracting this
information from an ObjectId.
• The next three bytes of an ObjectId are a unique identifier of the machine on
which it was generated.
• This is usually a hash of the machine’s hostname.
• By including these bytes, we guarantee that different machines will not generate colliding
ObjectIds.
• To provide uniqueness among different processes generating ObjectIds
concurrently on a single machine
• The next two bytes are taken from the process identifier (PID) of the ObjectId-
generating process.
• These first nine bytes of an ObjectId guarantee its uniqueness across machines
and processes for a single second.
• The last three bytes are simply an incrementing counter that is responsible for
uniqueness within a second in a single process.
• This allows for up to 256 (16,777,216) unique ObjectIds to be generated per
3

process in a single second.


Working with Mongo Shell – Lab Session
Moving to Creating, Updating
and Deleting Documents
module

You might also like