Mongo DB Exp 1-Content Beyond The Syllabus

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Database Management System Lab T E (E&TC)

Experiment No :
Content Beyond the Syllabus

Title Mango DB Queries using CRUD operations

Date of Performance :

PES’s Modern College of Engineering,


Department of Electronics & Telecommunication Engineering. 1
Database Management System Lab T E (E&TC)

Mango DB Queries using CRUD operations

AIM: Mongo DB queries: Design and Develop Mango DB Queries using CRUD
operations. (use CRUDoperations, SAVE Method and logical operators)

HARDWARE/ SOFTWARE REQUIREMENT:


 Any CPU with Pentium Processor or similar, 256 MB
 RAM or more, 1 GB Hard Disk or more.
 Windows 7 Operating System, Oracle 11g, Mango DB, Atlas Compass
THEORY:

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.

Difference Between SQL and NoSQL

PES’s Modern College of Engineering,


Department of Electronics & Telecommunication Engineering. 2
Database Management System Lab T E (ECE)

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.

Costly to scale. Cheaper to scale when compared to relational


databases.

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).

Example of SQL databases: MySQL, Oracle, Examples of NoSQL databases: MongoDB,


MS-SQL, SQLite. Apache CouchDB, Redis, HBase.

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

Scalable High-Performance Open-source, Document-orientated database.

• Built for Speed

• Rich Document based queries for Easy readability.

• Full Index Support for High Performance.

• Replication and Failover for High Availability.

• Auto Sharding for Easy Scalability.

• Map / Reduce for Aggregation.

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

• Data is stored as JSON style

PES’s Modern College of Engineering,


Department of Electronics & Telecommunication Engineering. 4
Database Management System Lab T E (E&TC)

• Index on any attribute

• Replication and High availability

MongoDB Terminologies for RDBMS concepts

RDBMS MongoDB

Database Database

Table, View Collection

Row Document (JSON, BSON)

Column Field

Index Index

Join Embedded Document

Foreign Key Reference

Partition Shard

Data Types of MongoDB

• 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)

• Code : This datatype is used to store javascript code into document.


• Regular expression : This datatype is used to store regular expression

Basic Database Operations

• use <database name>


switched to database provided with command
• db
To check currently selected database use the command db
• show dbs
Displays the list of databases
• db.dropDatabase()
To Drop the database
• db.createCollection (name)
Ex:- db.createCollection(Stud)
To create collection
• >show collections

List out all names of collection in current database

• 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()

MongoDB'sdb.collection.drop() is used to drop a collection from the database.

CRUD Operations:

• Insert
• Find
• Update
• Delete

CRUD Operations – Insert

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)

PES’s Modern College of Engineering,


Department of Electronics & Telecommunication Engineering. 6
Database Management System Lab T E (E&TC)

Example

>db.stud.insert({name: “Jiya”, age:15})

_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.

Insert a Document without Specifying an _id Field


• db.stud.insert( { Name : “Reena", Rno: 15 } )
• db.stud.find()
{ "_id" : "5063114bd386d8fadbd6b004”, “Name” : “Reena", “Rno”: 15 }

Insert a Document Specifying an _id Field


• db.stud.insert({ _id: 10, Name : “Reena", Rno: 15 } )
• db.stud.find()
{ "_id" : 10, “Name” : “Reena", “Rno”: 15 }

Insert Single Documents

db.stud.insert( {Name: “Ankit”, Rno:1, Address: “Pune”} )

Insert Multiple Documents


db.stud.insert( [
{ Name: “Ankit”, Rno:1, Address: “Pune”} ,
{ Name: “Sagar”, Rno:2},
{ Name: “Neha”, Rno:3}
])

Insert Multicolumn attribute


db.stud.insert( {
Name: “Ritu",
Address: { City: “Pune", State: “MH” },
Rno: 6
})

Insert Multivalued attribute


db.stud.insert( {
Name : “Sneha",
Hobbies: [“Singing”, “Dancing” , “Cricket”] ,
Rno:8
})

Insert Multivalued with Multicolumn attribute


db.stud.insert( {
Name : “Sneha",
Awards: [ { Award : “Dancing”, Rank: “1st”, Year: 2008 },

PES’s Modern College of Engineering,


Department of Electronics & Telecommunication Engineering. 7
Database Management System Lab T E (E&TC)

{Award : “Drawing”, Rank: “3rd”, Year: 2010 } ,


{Award : “Singing”, Rank: “1st”, Year: 2015 } ],
Rno: 9 })

CRUD Operations – Find


The find() Method- To display data from MongoDB collection. Displays all the documents in a
non structured way.
Syntax
>db.COLLECTION_NAME.find()

The pretty() Method- To display the results in a formatted way, you can use pretty() method.
Syntax
>db.COLLECTION_NAME.find().pretty()

Specify Equality Condition


use the query document { <field>: <value> }
Examples:
• db.stud.find( name: “Jiya" } )
• db.stud.find( { _id: 5 } )

Comparison Operators
Operator Description
$eq Matches values that are equal to a specified value.
$gt Matches values that are greater than a specified value.

$gte values that are greater than or equal to a specified value.


$lt Matches values that are less than a specified value.
$lte Matches values that are less than or equal to a specified value.
$ne Matches all values that are not equal to a specified value.
$in Matches any of the values specified in an array.
$nin Matches none of the values specified in an array.

Find Examples with comparison operators

db.stud.find( { rno: { $gt:5} } ) Shows all documents whose rno>5



db.stud.find( { rno: { $gt: 0, $lt: 5} } ) Shows all documents whose rno greater than 0 and

less than 5
Examples to show only particular columns

• db.stud.find({name: “Jiya”},{Rno:1}) To show the rollno of student whose name is equal to


Jiya (by default _id is also shown)
• db.stud.find({name: “jiya”},{_id:0,Rno:1}) show the rollno of student whose name is equal
to Jiya (_id is not shown)

Examples for Sort function


• db.stud.find().sort( { Rno: 1 } )
Sort on age field in Ascending order (1)

PES’s Modern College of Engineering,


Department of Electronics & Telecommunication Engineering. 8
Database Management System Lab T E (E&TC)

• db.stud.find().sort( { Rno: -1 } )
Sort on age field in Ascending order(-1)

Examples of Count functions

• db.stud.find().count()
Returns no of documents in the collection

Examples of limit and skip


• db.stud.find().limit(2)
Returns only first 2 documents
• db.stud.find().skip(5)
Returns all documents except first 5 documents

CRUD Operations – Update

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}})

3>To remove a age column from single document where id=100

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}})

CRUD Operations – Remove

• Remove All Documents


• db.inventory.remove({})
• Remove All Documents that Match a Condition
• db.inventory.remove ( { type : "food" } )
• Remove a Single Document that Matches a Condition
• db.inventory.remove ( { type : "food" }, 1 )

CONCLUSION:

PES’s Modern College of Engineering,


Department of Electronics & Telecommunication Engineering. 10
Database Management System Lab T E (E&TC)

OUTPUT:

PES’s Modern College of Engineering,


Department of Electronics & Telecommunication Engineering. 11
Database Management System Lab T E (E&TC)

PES’s Modern College of Engineering,


Department of Electronics & Telecommunication Engineering. 12
Database Management System Lab T E (E&TC)

PES’s Modern College of Engineering,


Department of Electronics & Telecommunication Engineering. 13

You might also like