0% found this document useful (0 votes)
5 views8 pages

Full Stack-UNIT 3

The document provides an overview of MongoDB, a NoSQL database that allows for high-performance data storage and retrieval without the constraints of traditional SQL databases. It explains the structure of MongoDB, including collections and documents, and highlights the flexibility of its schema compared to relational databases. Additionally, it discusses the various data types supported by MongoDB and differentiates between SQL and NoSQL databases.

Uploaded by

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

Full Stack-UNIT 3

The document provides an overview of MongoDB, a NoSQL database that allows for high-performance data storage and retrieval without the constraints of traditional SQL databases. It explains the structure of MongoDB, including collections and documents, and highlights the flexibility of its schema compared to relational databases. Additionally, it discusses the various data types supported by MongoDB and differentiates between SQL and NoSQL databases.

Uploaded by

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

UNIT-III

MongoDB

Understanding NoSQL and MongoDB


• At the core of most large-scale web applications and services is a high-performance data storage solution.
• The backend data store is responsible for storing everything from user account information to shopping cart items to blog and comment
data.
• Good web applications must store and retrieve data with accuracy, speed, and reliability. Therefore, the data storage mechanism you
choose must perform at a level that satisfies user demand.
• Several different data storage solutions are available to store and retrieve data needed by your web applications.
• The three most common are direct file system storage in files, relational databases, and NoSQL databases.
• The following sections describe MongoDB and discuss the design considerations you need to review before deciding how to implement
the structure of data and configuration of the database.
Need of NoSQL?
The concept of NoSQL (Not Only SQL) consists of technologies that provide storage and retrieval without the tightly constrained models of
traditional SQL relational databases.
The motivation behind NoSQL is mainly simplified designs, horizontal scaling, and finer control of the availability of data.
NoSQL breaks away from the traditional structure of relational databases and allows developers to implement models in ways that more closely
fit the data flow needs of their systems.
This allows NoSQL databases to be implemented in ways that traditional relational databases could never be structured.
There are several different NoSQL technologies, such as HBase’s column structure, Redis’s key/value structure, and Neo4j’s graph structure.
Also MongoDB is one of the most popular and well supported NoSQL databases currently available.
Understanding MongoDB
MongoDB is a NoSQL database based on a document model where data objects are stored as separate documents inside a collection.
The motivation of the MongoDB language is to implement a data store that provides high performance, high availability, and automatic scaling.
MongoDB is simple to install and implement.
Understanding Collections
• MongoDB groups data together through collections.
• A collection is simply a grouping of documents that have the same or a similar purpose.
• A collection acts similarly to a table in a traditional SQL database, with one major difference. In MongoDB, a collection is not enforced
by a strict schema; instead, documents in a collection can have a slightly different structure from one another as needed.
• This reduces the need to break items in a document into several different tables, which is often done in SQL implementations.
Understanding Documents
• A document is a representation of a single entity of data in the MongoDB database.
• A collection is made up of one or more related objects.
• A major difference between MongoDB and SQL is that documents are different from rows. Row data is flat, meaning there is one column
for each value in the row.
• However, in MongoDB, documents can contain embedded subdocuments, thus providing a much closer inherent data model to your
applications.
• In fact, the records in MongoDB that represent documents are stored as BSON, which is a lightweight binary form of JSON,
with field:value pairs corresponding to JavaScript property:value pairs.
• These field:value pairs define the values stored in the document.
• That means little translation is necessary to convert MongoDB records back into the JavaScript object that you use in your Node.js
applications.
• For example, a document in MongoDB may be structured similarly to the following with name, version, languages, admin,
and paths fields:
{
name: "New Project",
version: 1,
languages: ["JavaScript", "HTML", "CSS"],
admin: {name: "Brad", password: "****"},
paths: {temp: "/tmp", project: "/opt/project", html: "/opt/project/html"}
}
Notice that the document structure contains fields/properties that are strings, integers, arrays, and objects, just like a JavaScript object. Table
11.1 lists the different data types that field values can be set to in the BSON document.
The field names cannot contain null characters, . (dots), or $ (dollar signs). Also, the _id field name is reserved for the Object ID. The _id field is
a unique ID for the system that is made up of the following parts:
A 4-byte value representing the seconds since the last epoch
A 3-byte machine identifier
A 2-byte process ID
A 3-byte counter, starting with a random value
The maximum size of a document in MongoDB is 16MB, which prevents queries that result in an excessive amount of RAM being used or
intensive hits to the file system. Although you may never come close, you still need to keep the maximum document size in mind when
designing some complex data types that contain file data.
MongoDB Data Types
The BSON data format provides several different types that are used when storing the JavaScript objects to binary form. These types match the
JavaScript type as closely as possible. It is important to understand these types because you can actually query MongoDB to find objects that
have a specific property that has a value of a certain type. For example, you can look for documents in a database whose timestamp value is
a String object or query for ones whose timestamp is a Date object.
MongoDB assigns each of the data types an integer ID number from 1 to 255 that is used when querying by type. Table 11.1 shows a list of the
data types that MongoDB supports along with the number MongoDB uses to identify them.
Table 11.1 MongoDB data types and corresponding ID number
Type Number
Double 1
String 2
Object 3
Array 4
Binary data 5
Object id 7
Boolean 8
Date 9
Null 10
Regular Expression 11
JavaScript 13
JavaScript (with scope) 15
32-bit integer 16
Timestamp 17
64-bit integer 18
Decimal126 19
Min key -1
Max key 127
Another thing to be aware of when working with different data types in MongoDB is the order in which they are compared. When comparing
values of different BSON types, MongoDB uses the following comparison order from lowest to highest:
1. Min Key (internal type)
2. Null
3. Numbers (32-bit integer, 64-bit integer, Double)
4. String
5. Object
6. Array
7. Binary Data
8. Object ID
9. Boolean
10. Date, Timestamp
11. Regular Expression
12. Max Key (internal type)
Data Description
Types

String String is the most commonly used datatype. It is used to store data. A string must be UTF 8 valid in mongodb.

Integer Integer is used to store the numeric value. It can be 32 bit or 64 bit depending on the server you are using.

Boolean This datatype is used to store boolean values. It just shows YES/NO values.

Double Double datatype stores floating point values.

Min/Max
This datatype compare a value against the lowest and highest bson elements.
Keys

Arrays This datatype is used to store a list or multiple values into a single key.

Object Object datatype is used for embedded documents.

Null It is used to store null values.

Symbol It is generally used for languages that use a specific type.


This datatype stores the current date or time in unix time format. It makes you possible to specify your own date time by
Date
creating object of date and pass the value of date, month, year into it.

Differentiate SQL and NOSQL/MongoDB

Index SQL NoSQL

Databases are categorized as Relational NoSQL databases are categorized as Non-


1)
Database Management System (RDBMS). relational or distributed database system.

SQL databases have fixed or static or


2) NoSQL databases have dynamic schema.
predefined schema.

NoSQL databases display data as collection


SQL databases display data in form of tables
3) of key-value pair, documents, graph
so it is known as table-based database.
databases or wide-column stores.

4) SQL databases are vertically scalable. NoSQL databases are horizontally scalable.

In NoSQL databases, collection of


SQL databases use a powerful language
documents are used to query the data. It is
5) "Structured Query Language" to define and
also called unstructured query language. It
manipulate the data.
varies from database to database.
NoSQL databases are not so good for
SQL databases are best suited for complex
6) complex queries because these are not as
queries.
powerful as SQL queries.

SQL databases are not best suited for NoSQL databases are best suited for
7)
hierarchical data storage. hierarchical data storage.

MongoDB, BigTable, Redis, RavenDB,


MySQL, Oracle, Sqlite, PostgreSQL and MS-
8) Cassandra, Hbase, Neo4j, CouchDB etc. are
SQL etc. are the example of SQL database.
the example of nosql database

You might also like