In this exercise, you’ll use Visual Query Builder to build and run a query that retrieves documents from the
customers collection. The query will be based on two values (
Web Design) in the
To build the query in Visual Query Builder
1. Return to the Visual Query Builder form on the customers collection tab. The query you built in the previous exercise should still populate the query form.
2. In the Query section, click the Delete button (), which is located to the right of the two drop-down lists in the search condition section.
When you click the Delete button, Studio 3T removes the search condition, but leaves the Projection section untouched.
3. In the Query section, make sure the Match all of ($and) option from the drop-down list is selected – it should be set to it by default. When you select this option, Studio 3T removes the Array drop-down list.
4. Click the Add AND/OR group link near the bottom right corner of the Query section. Studio 3T adds a subsection for defining the $and operator’s first search condition. The subsection has a light gray background, as shown in the following figure.
5. In the new subsection, select Has array element(s) matching from the drop-down list. Studio 3T adds the Array drop-down list to the top of the subsection, similar to what you saw in the previous exercise.
6. From the newly added Array drop-down list, select the interests field.
7. Click the plus icon in the box at the bottom of the subsection. The form will expand to include options for defining the search condition.
8. In the first drop-down list in the expanded area, ensure that <Array Element> is selected, and in the second drop-down list (to the right of the first one), ensure that equals is selected.
9. In the text box beneath the two drop-down lists, type Database. The Query section should now look like the following figure.
The box beneath the subsection you just configured is outlined in red to show where you define the
$and operator’s second search condition.
10. In the box for the second search condition, click the Add AND/OR group link. (Be sure not to click the Add AND/OR group link directly above it.) This adds a second subsection, as shown in the following figure.
11. In the new subsection, select Has array element(s) matching from the drop-down list.
12. From the Array drop-down list, select the interests field.
13. In the same subsection, click the plus icon. The subsection will expand to include options for defining the second search condition.
14. In the first drop-down list in the expanded section, ensure that <Array Element> is selected, and in the second drop-down list, ensure that equals is selected.
15. In the text box beneath the two drop-down lists, type Web Design. The Query section should now look like the following figure.
16. Ensure that the Projection section still specifies that
interests fields, just like you left it at the end of the previous exercise.
17. On the query form, click the Run button in the upper right corner.
18. In the main window of the customers collection tab, ensure that the Result tab is selected. The query should return only two rows, as shown in the following figure.
19. Go to the Query Code tab and view the mongo shell code. It should look similar to the one of the
find statements you created in the first exercise, as shown in the following figure.
20. Close Studio 3T.Next – Test your skills: Querying Arrays Using MongoDB $elemMatch