MongoDB is opensource DB, CRUD with MongoDB is not as same with other DB using SQL statements it can be achieved using NoSQL json queries which i have try explained here.
2. Installation
Connection with MongoDB
Insert in MongoDB
Fetching data from MongoDB
The “Where” clause
Greater than, less than , equals ,and, or operators
Update and Upsert in MongoDB
Removing documents in collections.
Q & A
AgendaAgenda
3. Are you good in Copy Paste?Are you good in Copy Paste?
Get the binaries of your OS flavor fromGet the binaries of your OS flavor from https://https://
www.mongodb.org/downloadswww.mongodb.org/downloads
Unzip the file copy theUnzip the file copy the binbin folder and paste in desiredfolder and paste in desired
location.location.
Your MongoDB is ready to launch.Your MongoDB is ready to launch.
InstallationInstallation
4. MongoDB starts dataserver in mongod daemon , youMongoDB starts dataserver in mongod daemon , you
should pass atleast data directory to start this binariesshould pass atleast data directory to start this binaries
By default MongoDB starts with 27017 port anyhow youBy default MongoDB starts with 27017 port anyhow you
can change it usingcan change it using --port--port while starting the daemonwhile starting the daemon
MongoDB have lot many startup parameter which youMongoDB have lot many startup parameter which you
can see incan see in mongod --helpmongod --help command.command.
Connecting to MongoDBConnecting to MongoDB
5. CRUD stands for create read update and delete.CRUD stands for create read update and delete.
MongoDB has flexible queries to operation on crud.MongoDB has flexible queries to operation on crud.
CRUDCRUD
6. Insertion of document to mongoDB can be achievedInsertion of document to mongoDB can be achieved
through insert() method.through insert() method.
mongoDB is freiendly as it will create database as well asmongoDB is freiendly as it will create database as well as
collection if you inserting the first document in thecollection if you inserting the first document in the
collection.collection.
MongoDB will automatically insert _id field will beMongoDB will automatically insert _id field will be
automatically inserted for you, as a primary key if you doautomatically inserted for you, as a primary key if you do
not explicitly entered value in it.not explicitly entered value in it.
Mongo is Schema less ( not a oracle term of schema)Mongo is Schema less ( not a oracle term of schema)
No need to worry about the altering the table structureNo need to worry about the altering the table structure
INSERTINSERT
7. Retrieving the data is main method in any DBMS,Retrieving the data is main method in any DBMS,
You can retrieve data from MongoDB using find()You can retrieve data from MongoDB using find()
methodmethod
Find method you can pass mainly 2 argumentsFind method you can pass mainly 2 arguments
Condition of the data ( the Where clause)Condition of the data ( the Where clause)
Selection of the field ( as a Boolean value)Selection of the field ( as a Boolean value)
Sorting is separate method you can callSorting is separate method you can call
By default find() method displays the output in oneBy default find() method displays the output in one
line you can make is readable using pretty()line you can make is readable using pretty()
method.method.
FetchingthedataFetchingthedata
8. Where clause is the first argument in remove read andWhere clause is the first argument in remove read and
update method.update method.
You can define your conditions in this clause.You can define your conditions in this clause.
andand operation is easy whereoperation is easy where oror operator is some what trickyoperator is some what tricky
The WHERE clauseThe WHERE clause
SQL NoSQL
Select * from emp where
salary = 2000
Db.emp.find({salary:2000})
Select * from emp where
salary = 2000 and age =26
Db.emp.find({salary:2000,
age:26})
select * from emp where
salary = 2000 or age =26
db.emp.find({"$or":
[{"salary": 2000}, {"age":
26}]});
9. When we query we really don’t need all the column output,When we query we really don’t need all the column output,
when we interested in only few it is better to get only thosewhen we interested in only few it is better to get only those
field which will also avoid the full collection scanfield which will also avoid the full collection scan
In MongoDB this can be achieved in second argument ofIn MongoDB this can be achieved in second argument of
insert method.insert method.
This takes Boolean value 1 as true 0 as false.This takes Boolean value 1 as true 0 as false.
By default _id field will be 1 you can avoid displaying it byBy default _id field will be 1 you can avoid displaying it by
setting it 0.setting it 0.
Selecting fields to displaySelecting fields to display
SQL NoSQL
Select name from emp where salary =
2000
Db.emp.find({salary:2000},
{name:1,_id:0})
Select name,age from emp where
salary = 2000 and age =26
Db.emp.find({salary:2000, age:26}, },
{name:1,_id:0,age:1})
10. Comparison OperatorsComparison Operators
Select name from emp where sal > 10000
db.emp.find({sal : {$gt : 10000} })
Select name from emp where sal <= 50000
db.emp.find ( { sal : { $lte : 50000 } } )
Name Description
$gt Matches values that are greater than the value specified in the query.
$gte
Matches values that are greater than or equal to the value specified in
the query.
$lt Matches values that are less than the value specified in the query.
$lte
Matches values that are less than or equal to the value specified in the
query.
$ne Matches all values that are not equal to the value specified in the query.
$in Matches any of the values that exist in an array specified in the query.
$nin Matches values that do not exist in an array specified to the query.
11. Name Description
$or
Joins query clauses with a logical OR returns all documents that
match the conditions of either clause.
$and
Joins query clauses with a logical AND returns all documents that
match the conditions of both clauses.
$not
Inverts the effect of a query expression and returns documents that
do not match the query expression.
$nor
Joins query clauses with a logical NOR returns all documents that
fail to match both clauses.
Logical OperatorLogical Operator
Select * from emp where salary = 2000 and age =26
db.emp.find({$and : [{salary:2000}, {age:26}]})
select * from emp where salary = 2000 or age =26
db.emp.find({"$or": [{"salary": 2000}, {"age": 26}]});
12. MongoDB's update() methods are used to update
document into a collection.
>db.COLLECTION_NAME.update(SELECTIOIN_CRITERI
A, UPDATED_DATA)
Update emp set sal = 40000 where eid = a1115;
Db.emp.update({eid:a1115},{$set:{sal:40000}})
By default mongodb will update only single document, to
update multiple you need to set a paramter 'multi' to true.
Db.emp.update({eid:a1115},{$set:{sal:40000}},{multi:true})
Updating the documentsUpdating the documents
13. MongoDB's remove() method is used to remove document from
the collection. remove() method accepts two parameters. One is
deletion criteria and second is justOne flag
deletion criteria : (Optional) deletion criteria according to documents
will be removed.
justOne : (Optional) if set to true or 1, then remove only one
document.
Delete from emp where eid = a1115
Db.emp.remove({eid:a1115})
If there are multiple records and you want to delete only first
record, then set justOneparameter in remove() method
Db.emp.remove({eid:a1115},1)
If you want to truncate whole table just don’t pass any selection
criteria.
Db.emp.remove()
RemoveRemove