Professional Documents
Culture Documents
Nosql Notes101
Nosql Notes101
• 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
NOTES:
_id is default unless excluded explicity
db.comments.find(
{"name":"Lauren Carr"},
{"name": 1, "date": 0, "_id": 0}
)
- error!
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:
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)
NOTE:
db.movies.countDocuments({})
but:
Logical Operators
• $and operator
• $or Operator
• $nor Operator
• $not Operator
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.
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.