Exercise 3: Using Visual Query Builder to query embedded documents

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 return the documents for customers who have traveled to Poland.

To build the query in Visual Query Builder

1. In the Studio 3T Connection Tree, expand the sales database node and, if necessary, expand the Collections node.

2. Double-click the customers collection node to open the customers collection tab. 

3. On the customers collection tab, click the Visual Query Builder link to launch Visual Query Builder.

4. In the Query section of the query form, select the Has array element(s) matching option from the drop-down list in the upper left corner.

Studio 3T adds the Array drop-down list to the top of the Query section, as shown in the following figure.

Query tab

5. From the Array drop-down list, select the travel field. If the travel option is not listed, type the value directly into the text box.

6. Click the Add button in the box at the bottom of the Query section. The form will expand to include options for defining a search condition, as shown in the following figure.

Travel tab query

By default, the first drop-down list in the new search condition section shows <Array Element> as its value, and the second one shows equals as its value.

7. In the first drop-down list in the new search condition section, type country (replacing <Array Element>).

The fact that you must type the name of the value in the first box is an important differentiator from how you query arrays made up of other types of values. In those cases, you typically use the default <Array Element> option.

8. In the second drop-down list (the one to the right of the first list), ensure that the equals option is selected.

9. In the text box beneath the two drop-down lists, type Poland. Your query form should now look like the following figure.

Query tab

10. On the query form, click the Run button near the upper right corner. Studio 3T will run the query and list the returned documents on the Result tab in the main window of the customers collection tab. The query should have returned one document.

11. On the Result tab, display the collection in Tree View. 

12. Right-click the _id field and then click Expand Recursively. The document should now look similar to the following figure. Notice that the query returned the Maria document.

Result tab

13. Go to the Query Code tab and view the mongo shell code, which is shown in the following figure. As you can see, Studio 3T uses the $elemMatch operator even though you defined only one search condition.

Query code

14. Go to the IntelliShell tab, which should still be open from the previous exercise.

15. At the command prompt in the IntelliShell editor, replace the existing code with the following find statement:

db.customers.deleteMany( { _id: { $in: [ 1, 2, 3 ] } } );

The statement deletes the three documents that you added to the customers collection at the beginning of the first exercise. This will return the customers collection back to its original state.

16. On the IntelliShell toolbar, click the Execute entire script button. The statement should return a message indicating that the documents have been deleted.

17. Close the IntelliShell tab. If prompted to save your changes, click No.

18. 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