Unit-1 Notes
Unit-1 Notes
Unit-
Unit-1: Concepts of NoSQL: MongoDB
1.1 concepts of NoSQL. Advantages and features :
4)Graph-Based:
This Type of Database uses nodes and edges to store the data where entities
become nodes and their relation become edges . Every node and edges have
unique identifiers. These database are Multi-relational and traversing relations is
easier and faster since there is no need to calculate them.
Eg: Neo4J,Infinite Graph,FlockDB
CAP Theorem:
This is also called Brewer’s Theorem that states that a NoSQL database can
never guarantee All three properties at the same time. i.e it is impossible for a
distributed database to provide more than two of the three guarantees :
1) Consistency: The data should remain consistent even after the execution of an
operation. This means once data is written, any future read request should contain
that data.
2) Availability: The database should always be available and responsive. It should
not have any downtime.
3) Partition Tolerance: Partition Tolerance means that the system should continue
to function even if the communication among the servers is not stable, i.e if there
is unavailability in one part , other parts must remain unaffected.
Eventual Consistency:
Consistency: this term refers that there must be replicas created for the
data store on multiple machines to get availability and scalability. Changes made
on one replica must be propagated to others but it may take time. Since data may
be immediately updated on some whereas it may take time to update on others,
making it consistent not immediately but with course of time making it “Eventually
Consistent”.
Unlike ACID standards used by Relational Databases , the NoSQL databases use the
BASE standards where :
B : Basically
A : Available
S : Soft State
E : Eventually Consistent.
FEATURES OF NoSQL :
1) Non-
Non-relational :
NoSQL databases are non-relational and so there is no flat fixed-column
records. They work with self-contained aggregates or BLOBS. Also they do
not require complex features like Query languages , Joins , ACID , data-
normalization , relation mapping etc.
2) Schema-
Schema-free :
They are schema free or have relaxed schemas i.e not fixed . they do not
need to define any kind of table schema for the data. Data is usually stored
in heterogeneous Structures.
3) Simple API :
They have easy to use interfaces for storage and Querying results. As well
APIs provide low level data manipulation and selection modes. They use
HTTP REST with JSON.
4) Distributed :
Multiple NoSql databases can be executed in distributed fashion. Share
Nothing Architecture is used in NoSQL so there is less coordination and
more distribution. Provides auto-scaling and fail-over capabilities.
ADVANTAGES OF NoSQL:
This asserts that a single part of program can not effact entire system
from working. i.e changes in single part of data store does not affect
other parts of data store and ultimately entire database.
2) Easy Replication:
5) Horizontally Scalable:
Terminologies :
Document: a document is a set of key value pairs. They have dynamic schemas
i.e documents in same collection do not need to have same set of fields or
structure.
Advantages of mongoDB:
Syntax:
use database_name
example:
use mydb
Syntax:
db.dropDatabase()
example:
use mydb
db.dropDatabase()
1)Creating a collection:
To create a collection in mongoDB we use the createCollection()
method.
Syntax : db.createCollection(name,[options])
Optional arguments :
Eg1 : db.createCollection(“Mycollection”)
Eg2:
db.createCollection(“Mycollection”,{capped:true,autoIndexID:true,size:3
1696,max:1000})
mongoDB also creates collection on its own if we use insert command to
insert some document.
2)Drop a collection:
The drop() method is used to drop a collection from database in mongoDB.
Syntax : db.collection_name.drop()
Eg: use mydb
db.mycollection.drop()
1.3 CRUD operations (Insert, update, delete, find, Query and Projection
operators):
1) Insert Operation :
To insert data into mongoDB collection , we can use several insert() methods
or save() method.
• Insert() method :
This is used to insert a document into a collection.
Syntax : db.collection_name.insert(document)
Example : db.users.insert({_id:1,name:”rahil”,age:”20”})
• Save() method:
This method can be used to insert a document if the _id is not
specified. Else it would replace data of document containing _id
specified with that specified in the method.
Syntax : db.collection_name.save({New document})
Example : db.students.save({”email”:”[email protected]”})
• insertOne() method:
This method is used when only one document is to be inserted
into collection.
Syntax : db.collection_name.insertOne(document)
Example : db.students.insertOne({ Name:”Rahil” , class:”TYBCA”
,age:20})
• insertMany() method:
This method is used to insert multiple documents into
collection. You need to pass an array of documents into this
method.
Syntax :
db.collection_name.insertMany([doc1,doc2,doc3…docN])
Example:db.students.insertMany([{ Name:”Rahil” ,
class:”TYBCA” ,age:20},{ Name:”Vivek” , class:”TYBTech”
,age:20}])
2) Update Operation:
• Update() method:
This method updates the values in existing document.
Syntax : db.collection_name.update({selection
criteria},{$set:{new data to be updated}})
Example:db.students.update({‘name’:’rahil’},{$set:{‘age’:’21’}})
• Save() method:
This method can be used to update records as well as insert
records.This method replaces the existing document with new
document passed as the argument.
Syntax : db.collection_name.save({_id:id,New document})
Example :
db.students.save({_id:1,”email”:”[email protected]”})
• findOneAndUpdate() method:
It updates data in the first matched document that matches
selection criteria with the new data provided in parameter.
Even if multiple documents match only first match is updated.
Syntax : db.collection_name.findOneAndUpdate({selection
criteria},{data to be updated},[optionals])
Example :
db.students.findOneAndUpdate({name:”rahil”},{$set:{“phone”:”
9054124121”}})
• updateOne() method:
This method is used to update the first matched document with
the collection based on the given query. This method updates
only one document at a time.
Syntax : db.collection.updateOne({selection
criteria},{$set:{updation data}})
Example :
db.employees.updateOne({“name”:”vivek”},{$set:{“post”:”man
ager”}})
• updateMany() method:
This method is used to update all the matches with new data
that match the given selection criteria.
Syntax : db.collection.updateMany({selection
criteria},{$opt:{updation data}})
Example:db.mycollection.updateMany({“post”:”manager”},{$inc
:{salary:1000}})
3) Delete Operation :
Example :
1)Removing all (unconditioned) : db.collection1.remove({})
2)Removing all (matching condition) :
db.collection1.remove({“post”:”HR”})
3)Remove one (matching condition) :
db.collection1.remove({“post”:”HR”},1)
4) Find/Query Operation :
In MongoDB the find() method is used to query out results/documents
from a collection. This method returns a cursor to the retrieved
documents.
Syntax : db.collection.find({query})
Examples :
1)All documents : db.collection2.find()
2)All matching the condition : db.collection2.find({“name”:”Rahil”})
• sort() method:
sort() method is used to sort the documents in a collection. This methods
accept documents containing list of fields along with their sorting
order.1=ASC|-1=DESC.
Syntax : db.mycol.find().sort({key:1|-1})
1. aggregate:
In simpler sense, aggregation commands are those that are used to
group values and perform operations on them together. For
reference we can say this as for example a combination of count()
or sum() with the group by clause in SQL.
Syntax:
db.runCommand({aggregate:collection,pipeline:[{operations:values
}]})
Example: db.runCommand({aggregate:”mycol”,pipeline:[{$project:
{tags:1},{$unwind:”$tags”},{$group:{_id:”tags”,count:{$sum:1}}}],cu
rsor:{}})
2. count:
count command is used to count the number of docs in a collection
or view. It returns a doc that contains count and value.
Syntax : db.runCommand({count:collection,[opt]})
Example : db.runCommand({count:’mycol’})
3. distin
distinct:
this is used to find distinct values for given field in a single collection
. it returns a doc with array of distinct values.
Syntax : db.runCommand({distinct:collection,key:field})
Example : db.runCommand({distinct:”cars”,key:”models”})
4. mapReduce:
the map reduce function is an alternative to the aggregate function,
where it uses a mapFunction to map the required key-value pairs
and a reduce function to reduce and return the output results
according to aggregation operation taking place and field.results are
stored in collection name provided in {out:}
Syntax:
db.collection.mapReduce(mapFunction,reduceFunction,{out:”newc
ollection”})
Example:
var mapFunction1=function(){emit(this.field1,this.field2)
var reduceFunction1=function(key,valueArr){return
Array.sum(valueArr)}
db.collection.mapReduce(mapFunction1,reduceFunction1,{out:”res
ults”}).
This function is used to map field2 to field1 for all records and then
perform sum of field2 values and return the results into a collection
named return.
Works similar to :
db.collection.aggregate([
{$group:{_id:”$field1”,value:{$sum:”$field2}}},
{$out:”results”}
])