In this exercise, you’ll use IntelliShell to create and run several queries that retrieve data from the customers
collection. The queries will be based on values in the interests
array.
The exercises in this section use the sales
database and the customers
collection. The first section in this course demonstrated how to create the database and import the collection. Refer to that section for details on how to set them up if you haven’t done so already.
To build the queries in IntelliShell
1. Launch Studio 3T and connect to MongoDB Atlas.
Don’t have a MongoDB Atlas instance? Here’s how to set up a free cluster, or connect to an existing MongoDB database or localhost instead.
2. In the Connection Tree, right-click the sales database node and click Open IntelliShell. Studio 3T opens IntelliShell in its own tab in the main window.
3. On the IntelliShell toolbar, de-select the Retain previous result tabs button and Raw shell mode button if either are selected. This will make it easier to view the results returned by the mongo shell commands in this exercise.
4. At the command prompt in the IntelliShell editor, replace the existing code with the following find
statement:
db.customers.find( { "interests": { $elemMatch: {$eq: "Database"} } }, { "last" : 1.0, "interests": 1.0 } );
The statement uses the $elemMatch
operator to match documents that contain the Database
value in the interests
array.
As long as any array element matches the operator’s search condition ({$eq: "Database"}
), the document will be returned. In other words, the results will include any document that has an array value that equals Database
.
The statement also limits the results to the last
and interests
fields, which are specified as the second argument passed to the find
method.
The 1.0
value associated with each field indicates that the field should be included in the results, as opposed to being excluded.
5. On the IntelliShell toolbar, click the Execute entire script button (). Studio 3T runs the
find
statement and returns the results in the bottom window, displaying them in the Find tab.
6. On the Find tab, display the results in Table View, if it’s not already selected.
7. If the values in the interests
array are not displayed, right-click one of the values in the interests column and then click Show Embedded Fields.
The query should have returned 22 documents. The following figure shows part of those results. Notice that the Database
value can appear in any position within the array.
8. At the command prompt in the IntelliShell editor, replace the existing code with the following find
statement:
db.customers.find( { interests: "Database" }, { "last" : 1.0, "interests": 1.0 } );
The statement returns the same results as the previous find
statement; however, you do not need to use the $elemMatch
operator because you’re defining only one search condition.
9. On the IntelliShell toolbar, click the Execute entire script button. The statement should return the same 22 rows as the previous example.
10. At the command prompt in the IntelliShell editor, replace the existing code with the following find
statement:
db.customers.find( { "interests.0": "Web Design" }, { "last" : 1.0, "interests": 1.0 } );
The statement uses dot notation (interest.0
) to specify the index position when verifying values in the interests
array.
MongoDB indexes are zero-based, so interests.0
refers to the first position in the interests
array. As a result, the statement will return only documents whose first value in that array is Web Design
.
When using dot notation in this way, you must enclose the field and index position in quotation marks.
If you refer to the preceding example, you’ll see that the interests
field name, when used alone, does not need to be enclosed in quotes, although quotation marks can be used.
11. On the IntelliShell toolbar, click the Execute entire script button. The query should return only three documents. For each document, the first value in the interests
array should be Web Design
.
12. At the command prompt in the IntelliShell editor, replace the existing code with the following find
statement:
db.customers.find( { interests: { $size: 4 } }, { "last" : 1.0, "interests": 1.0 } );
The statement uses the $size
operator to specify that the interests
array should include exactly four elements for the document to be returned.
13. On the IntelliShell toolbar, click the Execute entire script button. The query should return seven documents, each containing four values in the interests
array.
14. At the command prompt in the IntelliShell editor, replace the existing code with the following find
statement:
db.customers.find( { "$and": [ { "interests": { $elemMatch: { $eq: "Database" } } }, { "interests": { $elemMatch: { $eq: "Web Design" } } } ] }, { "last" : 1.0, "interests" : 1.0 } );
The statement uses the $and
operator to define two search conditions that must both evaluate to true for a document to be returned.
Each search condition uses the $elemMatch
operator to indicate that the interests
array must contain the specified values (Database
and Web Design
, respectively).
15. On the IntelliShell toolbar, click the Execute entire script button. The statement returns only two documents.
Each document includes the Database
and Web Design
values in the interests
array, as shown in the following figure.
16. At the command prompt in the IntelliShell editor, replace the existing code with the following find
statement:
db.customers.find( { "$and": [ { "interests": "Database" }, { "interests": "Web Design" } ] }, { "last" : 1.0, "interests" : 1.0 } );
The statement works just like the previous one, except that it does not use the $elemMatch
operator, helping to simplify the overall statement.
17. On the IntelliShell toolbar, click the Execute entire script button. You should receive the same results as the previous statement.
18. At the command prompt in the IntelliShell editor, replace the existing code with the following find
statement:
db.customers.find( { "interests": { $all: ["Database", "Web Design"] } }, { "last" : 1.0, "interests" : 1.0 } );
Rather than using the $and
operator, the statement uses the $all
operator, which provides a simple way to specify that returned documents should include certain values in the target array.
The values themselves are passed to the $all
operator as an array, which in this case includes the Database
and Web Design
elements.
19. On the IntelliShell toolbar, click the Execute entire script button. You should receive the same results as the previous two statements.
20. Close the IntelliShell tab. If prompted to save your changes, click No.