In this exercise, you’ll use IntelliShell to query the documents you added in the previous exercise. As with that exercise, your statements will be based on the values in the embedded documents.
In this exercise, however, you’ll also use conditional operators as part of the query search conditions.
To run the queries in IntelliShell
1. Return to the IntelliShell tab for the customers
collection. The tab should still be in place from the previous exercise.
2. The first task is to retrieve any documents in which the customer has visited a country more than three times. At the command prompt in the IntelliShell editor, replace the existing code with the following find
statement:
db.customers.find({ "travel.visits": { $gt: 3 } } );
The statement uses the greater than ($gt
) operator to identify any customers who have visited a country the specified number.
When you use a conditional operator in your search condition, you must include the operator and its value, enclosed in curly braces.
3. On the IntelliShell toolbar, click the Run entire script button. The statement returns the Chen
document, which is the only one with an embedded document that has a visits
value greater than 3
.
The statement also demonstrates that you do not need to use the $elemMatch
operator when you specify only one search condition.
However, you might still see it used in mongo shell statements. For example, Visual Query Builder automatically uses the operator even if you specify only one search condition, giving you a statement that looks similar to the following:
db.customers.find( { travel: { $elemMatch: { visits: { $gt: 3 } } } } );
4. The next task is to find customers who have visited a country more than two times and who have assigned that country a rating of at least 8.
At the command prompt in the IntelliShell editor, replace the existing code with the following find
statement:
db.customers.find({ "travel.visits": { $gt: 2 }, "travel.rating": { $gt: 8 } } );
The statement should return any document with a visits
value greater than 2
and a rating
value greater than 8
.
5. Click the Run entire script button and switch to JSON View. The statement returns all three documents, even though Gladys
is the only customer to have visited a country more than twice and to have rated that country higher than 8.
The query engine returns all three documents because the query doesn’t use the $elemMatch
operator, so the specified visits
and rating
values do not need to appear in the same embedded document.
As you saw in the first exercise, this can lead to unexpected results if your intent was that they be part of the same document.
6. Replace the existing code with the following find
statement:
db.customers.find( { travel: { $elemMatch: { visits: { $gt: 2 }, rating: { $gt: 8 } } } } );
This time the statement incorporates the $elemMatch
operator to return those documents with a visits
value greater than 2
and a rating
value greater than 8.
7. Run the entire script. The statement now returns only the Gladys
document because she is the only customer to meet the search criteria.
8. At the command prompt in the IntelliShell editor, replace the existing code with the following find
statement:
db.customers.find( { travel: { $elemMatch: { visits: { $gt: 2 }, rating: { $gt: 6, $lt: 9 } } } } );
The statement is similar to the previous one except for the rating
element, which now specifies that the value must be greater than 6
but less than 9
. MongoDB lets you specify multiple operators in a search condition to help narrow down your search.
9. Return to the IntelliShell toolbar, and click the Run entire script button.The statement returns both the Maria
and Chen
documents because they have each visited at least one country more than two times and rated that country either a 7 or 8.
10. At the command prompt in the IntelliShell editor, replace the existing code with the following find
statement:
db.customers.find( { travel: { $elemMatch: { country: "Thailand", visits: { $gt: 1 }, rating: { $gte: 9 } } } } );
The statement uses the greater than or equal to ($gte
) conditional operator to specify possible rating
values. The statement also demonstrates that you can use conditional operators along with basic string comparisons when defining the search conditions.
11. Run the entire script. The statement returns the Maria
and Gladys
documents because they have each visited Thailand more than once and have rated that country 9 or above.
12. Leave IntelliShell open for the next exercise.