Top Banner
{ { MongoDB CRUD MongoDB CRUD Using NoSQL Using NoSQL -Darshan J -Darshan J
15

MongoDB crud

Jul 18, 2015

Download

Technology

Darshan J
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MongoDB crud

{{MongoDB CRUDMongoDB CRUD

Using NoSQLUsing NoSQL-Darshan J-Darshan J

Page 2: MongoDB crud

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

Page 3: MongoDB crud

Are you good in Copy Paste?Are you good in Copy Paste? Get the binaries of your OS flavor from Get the binaries of your OS flavor from https://https://

www.mongodb.org/downloadswww.mongodb.org/downloads Unzip the file copy the Unzip the file copy the binbin folder and paste in desired folder and paste in desired

location.location. Your MongoDB is ready to launch.Your MongoDB is ready to launch.

InstallationInstallation

Page 4: MongoDB crud

MongoDB starts dataserver in mongod daemon , you MongoDB 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 you By default MongoDB starts with 27017 port anyhow you can change it using can change it using --port --port while starting the daemonwhile starting the daemon

MongoDB have lot many startup parameter which you MongoDB have lot many startup parameter which you can see in can see in mongod --helpmongod --help command. command.

Connecting to MongoDBConnecting to MongoDB

Page 5: MongoDB crud

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

Page 6: MongoDB crud

Insertion of document to mongoDB can be achieved Insertion of document to mongoDB can be achieved through insert() method.through insert() method.

mongoDB is freiendly as it will create database as well as mongoDB is freiendly as it will create database as well as collection if you inserting the first document in the collection if you inserting the first document in the collection.collection.

MongoDB will automatically insert _id field will be MongoDB will automatically insert _id field will be automatically inserted for you, as a primary key if you do automatically 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

Page 7: MongoDB crud

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 arguments Find 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 one By 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.

Fet ch ing th e d at aFet ch ing th e d at a

Page 8: MongoDB crud

Where clause is the first argument in remove read and Where 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. and and operation is easy where operation is easy where oror operator is some what tricky operator 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}]});

Page 9: MongoDB crud

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 those when 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 of In 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 by By 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})

Page 10: MongoDB crud

Comparison OperatorsComparison Operators

Select name from emp where sal > 10000db.emp.find({sal : {$gt : 10000} })

Select name from emp where sal <= 50000db.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.

Page 11: MongoDB crud

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 =26db.emp.find({$and : [{salary:2000}, {age:26}]})

select * from emp where salary = 2000 or age =26db.emp.find({"$or": [{"salary": 2000}, {"age": 26}]});

Page 12: MongoDB crud

MongoDB's update()  methods are used to update document into a collection. 

>db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, 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

Page 13: MongoDB crud

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

Page 14: MongoDB crud
Page 15: MongoDB crud