Exercise 2: Using IntelliShell’s auto-completion features to write queries

In this exercise, you will build two queries in IntelliShell. The first query changes the current database to BioData.  The second query retrieves document data from the bios collection. To create the second query, you will use IntelliShell’s built-in auto-completion features.

The queries in this exercise are very basic and are meant only to demonstrate how to use IntelliShell to build queries. In later courses, you’ll learn how to build more complex queries by taking advantage of other Studio 3T features.

To create the queries

1. Return to the IntelliShell tab, position your cursor at the end of the command you just ran, and press Enter to add a second line in the Editor.

2. On the new line, type the following command:

use BioData;

The command changes the current database context to BioData.

Ensure that your cursor is positioned to the right of the second command (on the same line) and click the Execute statement at cursor button (execute at cursor).

You should receive a message stating that IntelliShell has switched to the BioData database.

switched to biodata

3. Press Enter to add a third line to the Editor and type the following command:

db.

4. When you press the period, IntelliShell displays an auto-completion pop-up, as shown in the following figure.

Autofill form

If you don’t see auto-suggestions, double-check that you’ve clicked on the lightning bolt icon (Auto-completion icon) which triggers auto-completion.

The pop-up lists the collections and methods available to the current database object, as it’s accessed through the db variable. You can scroll to any item and then press Enter to add it to your command in the Editor.

5. In the pop-up, ensure that the bios collection is selected and then press Enter. The collection is added to the command.

6. In the Editor, type a period after db.bios. 

This will again launch the auto-completion pop-up, which lists the methods available to the bios collection object.

7. In the pop-up, scroll down to the find method.

A message box will appear to the right, displaying information about the method.

Find criteria

The description explains how to use the find method and provides examples of its use.

As you can see, the method returns documents in a collection. If no arguments are specified, the method returns all documents.

8. Press Enter to add the find method to the command.

When you add a method, IntelliShell also adds a set of parentheses, where you can insert arguments if needed. For this exercise, you will add one argument.

9. Within the parenthesis, type the following code as an argument, including the curly braces:

{contribs:"ALGOL"}

The argument defines a filter based on a specific field and value pair. As a result, only documents that contain the ALGOL value in the contribs field will be returned. At this point, your command should look similar to the following code:

db.bios.find({contribs:"ALGOL"})

10. At the end of the command, after the closing parentheses, type a period, followed by the letter p, as in .p.

This launches the auto-completion pop-up, displaying a list of methods that contain the letter p.

You can filter the list of available methods by typing one or more letters. For example, if you type .ad after the find method, the pop-up will display only methods that contain these letters, such as addOption and readConcern. You can then select a method from the filtered list.

From the pop-up, select the pretty method and press Enter.

select pretty

The pretty method displays the results in a more readable format. Your command should now look similar to the following code: 

db.bios.find({contribs:"ALGOL"}).pretty();

Notice the semicolon terminator has been added to the end of the command. It tells MongoDB that the command ends at this point.

In this situation, in which a single command appears on a single line, the semicolon is not necessary, but it can be useful when running multiple commands to clearly distinguish the end of each command, especially if those commands span multiple lines. Some developers like to include the semicolon for all commands as part of their best practices.

Do not do anything else with your query at this time. You will use it for the next exercise.

Terms you might not know:

argument: In programming, a value that is passed between programs, subroutines or functions. Arguments are independent items, or variables, that contain data or codes (source)

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