MongoDB Notes
MongoDB Notes
stored with
millisecond precision. Often you want a whole day, week, or month, making a range query necessary.
To query for documents where a key’s value is not equal to a certain value, you must use another
conditional operator, "$ne", which stands for “not equal.”
If you want to find all users who do not have the username “joe,” you can query for them using this:
$in
If you have more than one possible value to match for a single key, use an array of criteria with "$in".
For instance, suppose we were running a raffle and the winning ticket numbers were 725, 542, and 390. To find
all three of these documents, we can construct the following query:
"$in" is very flexible and allows you to specify criteria of different types as well as values. For example, if we
are gradually migrating our schema to use usernames instead of user ID numbers, we can query for either by
using this:
This matches documents with a "user_id" equal to 12345, and documents with a "user_id" equal to "joe".
If "$in" is given an array with a single value, it behaves the same as directly matching the value.
For instance, {ticket_no : {$in : [725]}} matches the same documents as {ticket_no : 725}.
$nin
The opposite of "$in" is "$nin", which returns documents that don’t match any of the criteria in the array.
If we want to return all of the people who didn’t win anything in the raffle, we can query for them with this:
This query returns everyone who did not have tickets with those numbers.
"$in" gives you an OR query for a single key, but what if we need to find documents where "ticket_no" is
725 or "winner" is true?
$or
For this type of query, we’ll need to use the "$or" conditional.
- With a normal AND-type query, you want to narrow down your results as far as possible in as few
arguments as possible.
- OR-type queries are the opposite: they are most efficient if the first arguments match as many
documents as possible.
- While "$or" will always work, use "$in" whenever possible as the query optimizer handles it more
efficiently.
$not
"$not" is a metaconditional: it can be applied on top of any other criteria.
As an example, let’s consider the modulus operator, "$mod". "$mod" queries for keys whose values, when
divided by the first value given, have a remainder of the second value:
The previous query returns users with "id_num"s of 1, 6, 11, 16, and so on. If we want, instead, to return users
with "id_num"s of 2, 3, 4, 5, 7, 8, 9, 10, 12, etc., we can use "$not":
NOTE:
"$not" can be particularly useful in conjunction with regular expressions to find all documents that don’t match
a given pattern (regular expression usage is described in the section “Regular Expressions”).
IMP: Simultaneous updates to same field are not allowed
Conditional Semantics
- If you look at the update modifiers in the previous chapter and previous query documents, you’ll notice
that the $-prefixed keys are in different positions.
- In the query, "$lt" is in the inner document; in the update, "$inc" is the key for the outer document.
This generally holds true: conditionals are an inner document key, and modifiers are always a key in the
outer document.
For example, to find all users between the ages of 20 and 30, we can query for both "$gt" and "$lt" on the
"age" key:
because it modifies "age" twice. With query conditionals, no such rule applies.
There are a few “meta-operators” that go in the outer document: "$and“, "$or“, and "$nor“.
This query would match documents with an "x" field both less than 1 and equal to 4.
Although these seem like contradictory conditions, it is possible to fulfill if the "x" field is an array: {"x" :
[0, 4]} would match.
IMP Note:
that the query optimizer does not optimize "$and" as well as other operators.
null
null behaves a bit strangely. It does match itself, so if we have a collection with the following documents:
> db.c.find()
{ "_id" : ObjectId("4ba0f0dfd22aa494fd523621"), "y" : null }
{ "_id" : ObjectId("4ba0f0dfd22aa494fd523622"), "y" : 1 }
{ "_id" : ObjectId("4ba0f148d22aa494fd523623"), "y" : 2 }
we can query for documents whose "y" key is null in the expected way:
However, null not only matches itself but also matches “does not exist.” Thus, querying for a key with the
value null will return all documents lacking that key:
Regular Expressions
"$regex" provides regular expression capabilities for pattern matching strings in queries.
For example, if we want to find all users with the name Joe or joe, we can use a regular expression to do case-
insensitive matching:
Regular expression flags (for example, i) are allowed but not required. If we want to match not only various
capitalizations of joe, but also joey, we can continue to improve our regular expression:
MongoDB uses the Perl Compatible Regular Expression (PCRE) library to match regular expressions; any
regular expression syntax allowed by PCRE is allowed in MongoDB. It is a good idea to check your syntax with
the JavaScript shell before using it in a query to make sure it matches what you think it matches.
Note
MongoDB can leverage an index for queries on prefix regular expressions (e.g., /^joey/).
Indexes cannot be used for case-insensitive searches (/^joey/i).
Querying Arrays
Querying for elements of an array is designed to behave the way querying for scalars does. For example, if the
array is a list of fruits, like this:
will successfully match the document. We can query for it in much the same way as we would if we had a
document that looked like the (illegal) document: {"fruit" : "apple", "fruit" : "banana", "fruit" :
"peach"}.
$all
If you need to match arrays by more than one element, you can use "$all".
Then we can find all documents with both "apple" and "banana" elements by querying with "$all":
For instance, {fruit : {$all : ['apple']} will match the same documents as {fruit : 'apple'}.
You can also query by exact match using the entire array.
However, exact match will not match a document if any elements are missing or superfluous.
If you want to query for a specific element of an array, you can specify an index using the syntax key.index:
Arrays are always 0-indexed, so this would match the third array element against the string "peach".
$size
A useful conditional for querying arrays is "$size", which allows you to query for arrays of a given size.
Here’s an example:
- "$size" cannot be combined with another $ conditional (in this example, "$gt"), but this query can be
accomplished by adding a "size" key to the document. Then, every time you add an element to the array,
increment the value of "size".
IMP: Store and Increment var for push
> db.food.update(criteria,
... {"$push" : {"fruit" : "strawberry"}, "$inc" : {"size" : 1}})
Unfortunately, this technique doesn’t work as well with the "$addToSet" operator.
> db.cars.find()
{ "_id" : 2, "name" : "Honda Civic", "make" : 2015, "owner" : { "first" : "raj", "last" : "Mah", "age" : 31 }, "age" : 0,
"buy_dt" : ISODate("2018-12-10T16:00:00Z") }
{ "_id" : 5, "name" : "Ram", "reviews" : [ "Best in its Class", "Beats the Heat", "Best Mileage per gallon", "Amazing Truck",
"Affordable", "Sturdy" ], "buy_dt" : ISODate("2018-04-03T16:00:00Z") }
{ "_id" : 1, "name" : "Kia Rio", "make" : 2016, "owner" : { "first" : "Chris", "last" : "lagali", "age" : 30 }, "buy_dt" :
ISODate("2019-01-12T16:00:00Z") }
{ "_id" : 3, "name" : "Chevy Volte", "make" : 2012, "owner" : { "first" : "Edgar", "last" : "Punay", "age" : 58 }, "buy_dt" :
ISODate("2018-10-07T16:00:00Z") }
{ "_id" : 4, "name" : "kia soul", "make" : 2018, "owner" : { "first" : "Jeff", "last" : "bezos", "age" : 48 }, "buy_dt" :
ISODate("2018-07-05T16:00:00Z") }
>
>
> while(cur.hasNext()){
... print(latest.name)
... }
Kia Rio
Honda Civic
>
> page2
{ "_id" : 5, "name" : "Ram", "reviews" : [ "Best in its Class", "Beats the Heat", "Best Mileage per gallon", "Amazing Truck",
"Affordable", "Sturdy" ], "buy_dt" : ISODate("2018-04-03T16:00:00Z") }
{ "_id" : 3, "name" : "Chevy Volte", "make" : 2012, "owner" : { "first" : "Edgar", "last" : "Punay", "age" : 58 }, "buy_dt" :
ISODate("2018-10-07T16:00:00Z") }