Nosql Query
Nosql Query
DOCUMENT
PC318
OBJECTIVES:
• Introduction to Querying
• Finding Documents:
• Projection:
• Conditional Operators:
• Logical Operators:
• Regular Expressions:
• Working with Arrays
• Nested Objects:
• Limit, Skip, and Sort:
MongoDB Query Structure
• MongoDB queries are based on JSON documents in which you write your
criteria in the form of valid documents
• With the data stored in the form of JSON-like documents, the
queries seem more natural and readable.
Finding Documents
• The most basic query in MongoDB is performed with the find()
function on the collection.
• When this function is executed without any argument, it returns all
the documents in a collection.
Finding Documents
Using FINDONE()
• MongoDB provides another function, called findOne(), that
returns only one matching record.
• This function is very useful when you are looking to isolate a
specific record.
Choosing the Fields for the Output
• This technique is called projection.
• Projection is expressed as a second argument to the find() or
findOne() functions
Finding the Distinct Fields
• The distinct() function is used to get the distinct or unique values
of a field with or without query criteria.
count()
• This function is used to return the count of the documents within a
collection or a count of the documents that match the given query.
countDocuments()
• This function returns the count of documents that are matched by
the given condition.
estimatedDocumentCount()
• This function returns the approximate or estimated count of
documents in a collection.
• It does not accept any query and always returns the count of all
documents in the collection.
Conditional Operators
• Equals ($eq)
• Not Equal To ($ne)
• Greater Than ($gt) and Greater Than or Equal To ($gte)
• Less Than ($lt) and Less Than or Equal To ($lte)
• In ($in) and Not In ($nin)
Conditional Operators ($eq)
Conditional Operators ($ne)
Conditional Operators ($gt) and ($gte)
Conditional Operators ($lt) and ($lte)
Conditional Operators ($in) and ($nin)
Logical Operators
• $and operator
• $or Operator
• $nor Operator
• $not Operator
Logical Operators ($and)
Logical Operators ($or)
Logical Operators ($nor)
Logical Operators ($not)
Logical Operators ($not)
Regular Expressions
• A regular expression is a special string that defines a character
pattern.
• When such a regular expression is used to find string fields, all the
strings that have the matching pattern are found and returned.
Regular Expressions (Using the caret (^) operator)
• To find only the strings that start with the given regular
expression, the caret operator (^) can be used.
Regular Expressions (Using the dollar ($) operator )
• Similar to the caret operator, you can also match the strings that
end with the given regular expression.
Regular Expressions (Case-Insensitive Search)
• Searching with regular expressions is case-sensitive by default.
• MongoDB provides the $options operator for this, which can be
used for case-insensitive regular expression searches.
Query Arrays and Nested Documents
• The arrays and nested documents help store self-contained
information.
• The MongoDB query language allows us to query such complex
structures in the most intuitive manner.
Query Arrays and Nested Documents (Finding an Array by an Element)
Query Arrays and Nested Documents (Searching an Array with the $all Operator )
• The $all operator finds all those documents where the value of the
field contains all the elements, irrespective of their order or size:
Query Arrays and Nested Documents (Searching an Array with the $all Operator )
346
126
Query Arrays and Nested Documents (Searching an Array with the $all Operator )
1247
Query Arrays and Nested Documents (using $slice)
• The $slice operator is used to limit the array elements based on
their index position.
• This operator can be used with any array field, irrespective of the
field being queried or not.
Query Arrays and Nested Documents (using $slice)
Query Arrays and Nested Documents (using $slice)
Query Arrays and Nested Documents (using $slice)