Exercise 3: Using Visual Query Builder to query multiple array values

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 (Database and Web Design) in the interests array.

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 (Delete icon ), 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, select the Match all of ($and) option from the drop-down list beneath the Array drop-down list. 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.

Query code

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 Add button 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.

Query new tab

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.

Second tab

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 Add button. 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.

webdesign

16. Ensure that the Projection section still specifies that last and 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.

Result tab

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.

Query code

20. Close Studio 3T.

Log in

Log in to your Studio 3T Academy account

Forgot password?

Don't have an account yet? Sign up
Sign up

Start learning MongoDB today. All fields are mandatory.

By signing up for a course, you agree to the 3T Software Labs Privacy Policy.


Already have an account? Log in