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 double-click on the customers collection to open it. It will open a customers collection tab.
2. On the customers collection tab, click Visual Query Builder in the upper right-hand corner to launch it.
Our goal is to find documents for customers who’ve traveled to “Poland”.
3. 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.
Doing so adds an Array drop-down list to the top of the Query section.
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 plus icon 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.
By default, the first drop-down list in the new search condition section shows <Array Element> as its value, and the second drop-down list shows equals as its value.
7. In the first drop-down list in the new search condition section, type country (replacing <Array Element>).
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.
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.
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.
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. Click the Run 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.