SlideShare a Scribd company logo
REG EXP
Using $regex operator for Pattern matching
• The regex operator in MongoDB is used to search for specific
strings in the collection. The following example shows how
this can be done.Let's assume that we have our same
Employee collection which has the Field names of
"Employeeid" and "EmployeeName". Let' also assume that we
have the following documents in our collection.
Syntax:
db.Employee.find({EmployeeName : {$regex:
"Gu" }}).forEach(printjson)
Code Explanation:
• Here we want to find all Employee Names which
have the characters 'Gu' in it. Hence, we specify the
$regex operator to define the search criteria of 'Gu'
• The printjson is being used to print each document
which is returned by the query in a better way.
Mongo DB REG EXP and Logical Operator.pptx
MongoDB Logical Query Operator - $and $not $or
• Logical Operator - $and
• The MongoDB $and operator performs a logical
AND operation on an array of two or more
expressions and retrieves the documents which
satisfy all the expressions in the array.
• The $and operator uses short-circuit evaluation.
If the first expression (e.g. <expression1>)
evaluates to false, MongoDB will not evaluate
the remaining expressions.
Syntax:
{ $and: [ { <exp1> }, { <exp2> } , ... , { <expN> } ] }
Eg: If we want to select all documents from the collection
"student" which satisfying the condition -
1. sex of student is Female and
2. class of the student is VI and
3. grd_point of the student is greater than equal to 31
the following mongodb command can be used :
• db.student.find({$and:[{"sex":"Male"},{"grd_point":{ $gte:
31 }},{"class":"VI"}]}).pretty();
• Logical Operator - $not
• The MongoDB $not operator performs a logical
NOT operation on the given expression and
fetches selected documents that do not match
the expression and the document that do not
contain the field as well, specified in the
expression.
• Syntax:
• { field: { $not: { <expression> } } }
Example of MongoDB Logical Operator - $not
• If we want to select all documents from the
collection "student" which satisfying the
condition -
• age of the student is at least 12
the following mongodb command can be used :
• >db.student.find( {"age": { $not: {$lt :
12}}}).pretty();
MongoDB Evaluation Query operator - $where
The MongoDB $where operator is used to match
documents that satisfy a JavaScript expression. A
string containing a JavaScript expression or a
JavaScript function can be pass using the $where
operator. The JavaScript expression or function
may be referred as this or obj.
• ample collection "table3"
• { "_id" : objectId("52873b364038253faa4bbc0e"),
"student_id" : "STU002", "sem" : "sem1", "english" :
"A", "maths" : "A+", "science" : "A" }
• { "_id" : ObjectId("52873b5d4038253faa4bbc0f"),
"student_id" : "STU001", "sem" : "sem1", "english" :
"A+", "maths" : "A+", "science" : "A" }
• { "_id" : objectId("52873b7e4038253faa4bbc10"),
"student_id" : "STU003", "sem" : "sem1", "english" :
"A+", "maths" : "A", "science" : "A+" }
Example :If we want to select all documents
from the collection "table3" which satisfying the
condition -
• The grade of english must be same as science
the following mongodb command can be used :
• >db.table3.find( { $where: function() { return
(this.english == this.science) }}).pretty();

More Related Content

PPTX
Mongo Nosql CRUD Operations
PDF
Slide perkenalan dengan dasar MongoDB-query
PDF
PPTX
Mongo DB 102
PPS
MongoDB crud
PDF
Data Engineering: Arrays in Mongo DB(and operators)
PPTX
Introduction to MongoDB – A NoSQL Database
PPTX
Mongo DB Presentation
Mongo Nosql CRUD Operations
Slide perkenalan dengan dasar MongoDB-query
Mongo DB 102
MongoDB crud
Data Engineering: Arrays in Mongo DB(and operators)
Introduction to MongoDB – A NoSQL Database
Mongo DB Presentation

Similar to Mongo DB REG EXP and Logical Operator.pptx (20)

PPTX
Mongo db basic installation
PDF
MongoDB.pdf54teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeer
PPTX
Mongo db queries
PPTX
No SQL DB lecture showing structure and syntax
PDF
full stack modul 5, mongodb,webpack,front-end,back-end
PPTX
PPTX
UNIT-1 MongoDB.pptx
PDF
Mongo db
PPT
Mongo db basics
PPTX
MongoDB_ppt.pptx
PDF
Full metal mongo
KEY
MongoDB at GUL
PPTX
MongoDB Workshop.pptx computer science and engineering
PDF
Mongo db basics
PPTX
Important operator in Nosql MongoDB .pptx
PDF
Mdb dn 2016_06_query_primer
PPTX
MongoDB Knowledge share
PPTX
Document validation in MongoDB 3.2
TXT
Array operators
PPTX
MongoDB
Mongo db basic installation
MongoDB.pdf54teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeer
Mongo db queries
No SQL DB lecture showing structure and syntax
full stack modul 5, mongodb,webpack,front-end,back-end
UNIT-1 MongoDB.pptx
Mongo db
Mongo db basics
MongoDB_ppt.pptx
Full metal mongo
MongoDB at GUL
MongoDB Workshop.pptx computer science and engineering
Mongo db basics
Important operator in Nosql MongoDB .pptx
Mdb dn 2016_06_query_primer
MongoDB Knowledge share
Document validation in MongoDB 3.2
Array operators
MongoDB
Ad

Recently uploaded (20)

PDF
Introduction to Data Science and Data Analysis
PPTX
Introduction to Knowledge Engineering Part 1
PPTX
SAP 2 completion done . PRESENTATION.pptx
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked 2025}
PDF
Clinical guidelines as a resource for EBP(1).pdf
PPTX
Managing Community Partner Relationships
PDF
Lecture1 pattern recognition............
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PPTX
Computer network topology notes for revision
PDF
[EN] Industrial Machine Downtime Prediction
PPTX
Database Infoormation System (DBIS).pptx
PDF
Transcultural that can help you someday.
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
Introduction to Data Science and Data Analysis
Introduction to Knowledge Engineering Part 1
SAP 2 completion done . PRESENTATION.pptx
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Data_Analytics_and_PowerBI_Presentation.pptx
Qualitative Qantitative and Mixed Methods.pptx
Miokarditis (Inflamasi pada Otot Jantung)
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
Capcut Pro Crack For PC Latest Version {Fully Unlocked 2025}
Clinical guidelines as a resource for EBP(1).pdf
Managing Community Partner Relationships
Lecture1 pattern recognition............
oil_refinery_comprehensive_20250804084928 (1).pptx
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
Computer network topology notes for revision
[EN] Industrial Machine Downtime Prediction
Database Infoormation System (DBIS).pptx
Transcultural that can help you someday.
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
Ad

Mongo DB REG EXP and Logical Operator.pptx

  • 2. Using $regex operator for Pattern matching • The regex operator in MongoDB is used to search for specific strings in the collection. The following example shows how this can be done.Let's assume that we have our same Employee collection which has the Field names of "Employeeid" and "EmployeeName". Let' also assume that we have the following documents in our collection.
  • 3. Syntax: db.Employee.find({EmployeeName : {$regex: "Gu" }}).forEach(printjson) Code Explanation: • Here we want to find all Employee Names which have the characters 'Gu' in it. Hence, we specify the $regex operator to define the search criteria of 'Gu' • The printjson is being used to print each document which is returned by the query in a better way.
  • 5. MongoDB Logical Query Operator - $and $not $or • Logical Operator - $and • The MongoDB $and operator performs a logical AND operation on an array of two or more expressions and retrieves the documents which satisfy all the expressions in the array. • The $and operator uses short-circuit evaluation. If the first expression (e.g. <expression1>) evaluates to false, MongoDB will not evaluate the remaining expressions.
  • 6. Syntax: { $and: [ { <exp1> }, { <exp2> } , ... , { <expN> } ] } Eg: If we want to select all documents from the collection "student" which satisfying the condition - 1. sex of student is Female and 2. class of the student is VI and 3. grd_point of the student is greater than equal to 31 the following mongodb command can be used : • db.student.find({$and:[{"sex":"Male"},{"grd_point":{ $gte: 31 }},{"class":"VI"}]}).pretty();
  • 7. • Logical Operator - $not • The MongoDB $not operator performs a logical NOT operation on the given expression and fetches selected documents that do not match the expression and the document that do not contain the field as well, specified in the expression. • Syntax: • { field: { $not: { <expression> } } }
  • 8. Example of MongoDB Logical Operator - $not • If we want to select all documents from the collection "student" which satisfying the condition - • age of the student is at least 12 the following mongodb command can be used : • >db.student.find( {"age": { $not: {$lt : 12}}}).pretty();
  • 9. MongoDB Evaluation Query operator - $where The MongoDB $where operator is used to match documents that satisfy a JavaScript expression. A string containing a JavaScript expression or a JavaScript function can be pass using the $where operator. The JavaScript expression or function may be referred as this or obj.
  • 10. • ample collection "table3" • { "_id" : objectId("52873b364038253faa4bbc0e"), "student_id" : "STU002", "sem" : "sem1", "english" : "A", "maths" : "A+", "science" : "A" } • { "_id" : ObjectId("52873b5d4038253faa4bbc0f"), "student_id" : "STU001", "sem" : "sem1", "english" : "A+", "maths" : "A+", "science" : "A" } • { "_id" : objectId("52873b7e4038253faa4bbc10"), "student_id" : "STU003", "sem" : "sem1", "english" : "A+", "maths" : "A", "science" : "A+" }
  • 11. Example :If we want to select all documents from the collection "table3" which satisfying the condition - • The grade of english must be same as science the following mongodb command can be used : • >db.table3.find( { $where: function() { return (this.english == this.science) }}).pretty();