Mongo DB Exp 1-Content Beyond The Syllabus
Mongo DB Exp 1-Content Beyond The Syllabus
Mongo DB Exp 1-Content Beyond The Syllabus
Experiment No :
Content Beyond the Syllabus
Date of Performance :
AIM: Mongo DB queries: Design and Develop Mango DB Queries using CRUD
operations. (use CRUDoperations, SAVE Method and logical operators)
What is NoSQL?
NoSQL database stands for "Not Only SQL" or "Not SQL." Though a better term would be
"NoREL", NoSQL caught on. Carl Strozz introduced the NoSQL concept in 1998.
NoSQL (“non SQL” or “not only SQL”) databases were developed in the late 2000s with a focus on
scaling, fast queries, allowing for frequent application changes, and making programming
simpler for developers. Relational databases accessed with SQL (Structured Query Language)
were developed in the 1970s with a focus on reducing data duplication as storage was much
more costly than developer time. SQL databases tend to have rigid, complex, tabular schemas
and typically require expensive vertical scaling
NoSQL is a non-relational DBMS, that does not require a fixed schema, avoids joins, and is easy
to scale. The purpose of using a NoSQL database is for distributed data stores with humongous
data storage needs. NoSQL is used for Big data and real-time web apps. For example, companies
like Twitter, Facebook, Google collect terabytes of user data every single day.
Why NoSQL?
The concept of NoSQL databases became popular with Internet giants like Google, Facebook,
Amazon, etc. who deal with huge volumes of data. The system response time becomes slow when
you use RDBMS for massive volumes of data.
To resolve this problem, we could "scale up" our systems by upgrading our existing hardware.
This process is expensive.
SQL NoSQL
SQL databases are mainly relational database NoSQL databases are mainly non-relational or
(RDBMS). distributed databases.
SQL databases are table based in the form of NoSQL databases can be based on documents,
row & columns and must strictly adhere to key-value pairs, graphs or columns and they don’t
standard schema definitions. have to stick to standard schema definitions.
They are a better option for applications which
need multi-row transactions.
They have a well-designed pre-defined They have the dynamic schema for unstructured
schema for structured data. data. Data can be flexibly stored without having a
pre-defined structure.
SQL databases favors normalized schema. NoSQL databases favors de-normalized schema.
SQL databases are vertically scalable. They NoSQL databases are horizontally scalable. They
can be scaled by increasing the hardware can be scaled by adding more servers to the
capacity (CPU, RAM, SSD, etc.) on a single infrastructure to manage large load and lessen the
server. heap.
They are a good fit for complex queries as Not a good fit for complex queries as there is no
SQL has a standard interface for handling standard interface in NoSQL for handling queries.
queries. The queries in NoSQL are not as powerful as SQL
The syntax of SQL queries is fixed. queries.
It is called as UnQL, and the syntax for using the
Unstructured query language will vary from
syntax to syntax.
SQL databases do not suit well for NoSQL databases suit best for hierarchical data
hierarchical data storage. storage as it follows the key-value pair method for
storing the data.
From a commercial perspective, SQL They are classified on the basis of the way they
databases are generally classified as open store data as key-value store, document store,
source or closed source. graph store, column store, and XML store.
SQL databases properly follow ACID NoSQL databases properly follow Brewers CAP
properties (Atomicity, Consistency, Isolation theorem (Consistency, Availability, and Partition
& Durability). tolerance).
Excellent vendor support and community Only limited community support is available for
support is available for all SQL databases. NoSQL databases.
Best fit for high transaction-based You can use NoSQL for heavy transactional
PES’s Modern College of Engineering,
applications. purpose. However, it is not the best fit for this.
Department of Electronics & Computer Engineering. 3
Database Management System Lab T E (E&TC)
Not suitable for hierarchical data storage. Suitable for hierarchical data storage and storing
large data sets (E.g. Big Data).
Document-Oriented:
Document-Oriented NoSQL DB stores and retrieves data as a key value pair but the value part is
stored as a document. The document is stored in JSON or XML formats. The value is understood
by the DB and can be queried.
MongoDB
Advantages of MongoDB
• Schema less : Number of fields, content and size of the document can be differ from one
document to another.
• No complex joins
RDBMS MongoDB
Database Database
Column Field
Index Index
Partition Shard
• String : This is most commonly used datatype to store the data. String in mongodb must
be UTF-8 valid.
• Integer : This type is used to store a numerical value. Integer can be 32 bit or 64 bit
depending upon your server.
• Boolean : This type is used to store a boolean (true/ false) value.
• Double : This type is used to store floating point values.
• Min/ Max keys : This type is used to compare a value against the lowest and highest
BSON elements.
• Arrays : This type is used to store arrays or list or multiple values into one key.
• Timestamp :ctimestamp. This can be handy for recording when a document has been
modified or added.
• Object : This datatype is used for embedded documents.
• Null : This type is used to store a Null value.
• Symbol : This datatype is used identically to a string however, it's generally reserved for
languages that use a specific symbol type.
• Date : This datatype is used to store the current date or time in UNIX time format. You
can specify your own date time by creating object of Date and passing day, month, year
into it.
• Object ID : This datatype is used to store the document’s ID.
• Binary data : This datatype is used to store binay data.
PES’s Modern College of Engineering,
Department of Electronics & Telecommunication Engineering. 5
Database Management System Lab T E (E&TC)
• db.databasename.insert
({Key : Value})
Ex:- db.Stud.insert({{Name:”Jiya”})
In mongodb you don't need to create collection. MongoDB creates collection automatically, when
you insert some document.
• db.collection.drop()
Example:- db.Stud.drop()
CRUD Operations:
• Insert
• Find
• Update
• Delete
The insert() Method:- To insert data into MongoDB collection, you need to use MongoDB's
insert() or save()method.
Syntax
>db.COLLECTION_NAME.insert(document)
Example
_id Field
• If the document does not specify an_id field, then MongoDB will add the _id field and
assign a unique ObjectId for the document before inserting.
• The _id value must be unique within the collection to avoid duplicate key error.
The pretty() Method- To display the results in a formatted way, you can use pretty() method.
Syntax
>db.COLLECTION_NAME.find().pretty()
Comparison Operators
Operator Description
$eq Matches values that are equal to a specified value.
$gt Matches values that are greater than a specified value.
• db.stud.find().sort( { Rno: -1 } )
Sort on age field in Ascending order(-1)
• db.stud.find().count()
Returns no of documents in the collection
Syntax
db.CollectionName.update(
<query/Condition>,
<update with $set or $unset>,
{
upsert: <boolean>,
multi: <boolean>,
})
upsert
• If set to True, creates new document if no matches found.
multi
• If set to True, updates multiple documents that matches the query criteria
CRUD Operations
UpdateExamples
1>Set age = 25 where id is 100, First Whole document is replaced where condition is matched
and only one field is remained as age:25
db.stud.update(
{ _id: 100 },
{ age: 25})
2>Set age = 25 where id is 100, Only the age field of one document is updated where
condition is matched .
db.stud.update(
{ _id: 100 },
{ $set:{age: 25}})
db.stud.update(
PES’s Modern College of Engineering,
Department of Electronics & Telecommunication Engineering. 9
Database Management System Lab T E (E&TC)
{ _id: 100 },
{ $unset:{age: 1}})
CONCLUSION:
OUTPUT: