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.
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:
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 Run statement at cursor button ().
You should receive a message stating that IntelliShell has switched to the
3. Press Enter to add a third line to the Editor and type the following command:
4. When you press the period, IntelliShell displays an auto-completion pop-up, as shown in the following figure.
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
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
A message box will appear to the right, displaying information about the method.
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:
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:
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.
From the pop-up, select the
pretty method and press Enter.
pretty method displays the results in a more readable format. Your command should now look similar to the following code:
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)