You are on page 1of 11

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.

NOTES:

db.comments.find()
db.comments.find({})
db.comments.find({"a_non_existent_field" : null})
- Returns all the documents from the collections

db.comments.find({"name":"Lauren Carr"}).pretty()
- Returns a well-formatted result, easily readable

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.
NOTES:

db.comments.findOne()
db.comments.findOne({})
db.comments.findOne({"a_non_existent_field" : null})
- Returns the first document in your collection, the first one in line
IN MONGO SHELL:
var comments = db.comments.find({“name”: “Lauren Carr”})
- By using the variable, we can iterate through the elements
comments.next()
- Moves the cursor in the next index position and returns documents from there
- Cursor by default start from the beginning of the collections
comments.hasNext()
- If the cursor reaches the last document in the collection, calling next() returns error.
- hasNext() returns true if the collection has a document at the next index position

Choosing the Fields for the Output


• This technique is called projection.
• Projection is expressed as a second argument to the find() or findOne() functions

NOTES:
_id is default unless excluded explicity

db.comments.find(
{"name":"Lauren Carr"},
{"name": 1, "date": 0, "_id": 0}
)
- error!

Finding the Distinct Fields


• The distinct() function is used to get the distinct or unique values of a field with or without query
criteria.

NOTES:

db.movies.distinct()
- Error! As it needs a required key, distinct key
Note that distinct() always return as an array.

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.
NOTES
- According to the pdf, unlike count(), in countDocuments() a query argument is mandatory
Hence if we used this:

db.movies.countDocuments()
- It will return an error so we use this one:

db.movies.countDocuments({})
- To count all the documents in the collections
BUT I TRY THESE TWO IN VISUAL CODE AND:

(OR SAYOP LANG JUD KOG SABOT SA PPT BWHAHHA, GE BYE!)

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.

NOTE:
- count() is based on metadata so the results are less accurate but the performance is better,
on the other hand, the other two are more accurate but takes longer than count().
- estimatedDocumentCount() DOES NOT accept any query and always return 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)

- Both queries have the same effect

Conditional Operators ($ne)

Conditional Operators ($gt) and ($gte)

Conditional Operators ($lt) and ($lte)


Conditional Operators ($in) and ($nin)

NOTE:

db.movies.countDocuments({"nef": {$nin: ["a value", "another value"]}})


is same as:

db.movies.countDocuments({})
but:

db.movies.countDocuments({"nef": {$nin: ["a value", "another value", null]}})


returns 0 as it did not match any document, because non-existent field always has a value of null,
$nin condition did not stand true for any of the documents.

Logical Operators
• $and operator
• $or Operator
• $nor Operator
• $not Operator

Logical Operators ($and)

db.movies.countDocuments({"rated": "PG", "year" : 2004})


- Has the same result as the first pic
Logical Operators ($or)

NOTE:
- $in is used to determine whether a given field has at least one of the values provided in an
array, whereas the $or is not bound to any specific fields and accepts multiple expression.

Logical Operators ($nor)

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 (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)

You might also like