In this exercise, you’ll use IntelliShell to update and run the final find
statement you created in the previous exercise, as it was generated on the Query Code tab.
To build and run the find
statement
1. On the customers collection tab, go to the Query Code tab and click on the Open in IntelliShell icon (). This will open the mongo shell code directly in a separate IntelliShell tab.
2. In the newly created IntelliShell tab, remove the first use
statement.
3. On the IntelliShell toolbar, click the Execute entire script button. Studio 3T returns the results in the Find tab, as shown in the following figure.
4. You’ll now modify the query directly in IntelliShell, starting with the first search condition in the query argument. At the command prompt, replace the Oregon
value with Idaho
.
5. Next, you’ll add another search condition to the query argument. The new search condition will filter out any documents that do not include the user_name
field. To make the change, type a comma after the device
field and its value, insert a new line, and type the following expression:
"user_name" : { $exists : true }
The search condition includes the $exists
operator with its value set to true
. As a result, only documents that include the user_name
field will be returned.
The query argument should now look similar to the following code:
{ "address.state" : { "$in" : [ "Washington", "Idaho" ] }, "device" : /.*iphone.*/i, "user_name" : { $exists : true } }
6. The next step is to add the _id
field to the list of projection fields, with its value to 0
, rather than 1
, so the field is not included in the results.
To make the change, type a comma after the user_name
field and its value, insert a new line, and type the following expression:
"_id" : 0
The projection argument should now look similar to the following code:
{ "first" : 1.0, "last" : 1.0, "user_name" : 1.0, "_id" : 0 }
7. The final step is to change the sort order so it’s based on the user_name
column.
To make the change, remove the last
field, its value, and the trailing comma, and then change the first
field name to user_name
. The sort method should now look like the following code:
sort( { "user_name" : 1.0 }
8. On the IntelliShell toolbar, click the Execute entire script button. Studio 3T returns the results shown in the following figure.
9. Close the IntelliShell tab. If prompted to save your changes, click No.
10. Close the customers collection tab, but do not drop the collection or database. You’ll need them for the rest of this course.
11. Close Studio 3T.