Visual Query Builder helps you build MongoDB queries by dragging and dropping fields from the documents in a collection, perfect if you’re new to MongoDB and learning the MongoDB query syntax. Download Studio 3T and try the MongoDB query builder.
Open Visual Query Builder – Ctrl + B (⌘+ B)
Run Query – Ctrl + R (⌘+ R)
Switch to Tree View – Ctrl + Alt + 1 (⌥ + ⌘ + 1)
Switch to Table View – Ctrl + Alt + 2 (⌥ + ⌘ + 2)
Switch to JSON View – Ctrl + Alt + 3 (⌥ + ⌘ + 3)
Basics
Visual Query Builder is part of the Collection Tab, where you can view, query, and edit documents in a collection. You can drag and drop fields into Visual Query Builder to create a MongoDB query, and as you do this, you’ll see Studio 3T building the mongo shell syntax in the Query Bar.
To open Visual Query Builder:
- Button – Click on the Visual Query Builder button
- Right-click – Right-click anywhere in the Result tab and choose Visual Query Builder
- Shortcut – Press Ctrl + B (⌘+ B)
- Click-and-drag – Click and drag any cell in the Result tab and Visual Query Builder automatically opens
Build the MongoDB query
Let’s build a query using Studio 3T’s MongoDB query builder and the Customers collection. You can download the zip file here, then import the JSON file to your MongoDB database.
Drag and drop fields
Select the target field by clicking on any cell (not the header), then drag the selected field(s) and drop them into Visual Query Builder.
In Table View, you can select only one field at a time.
In Tree View, you can select multiple fields and drop them into Visual Query Builder. To do this, hold the CTRL key (or the command key) and click the fields.
As you build your MongoDB query, you’ll see Studio 3T writing the mongo shell syntax in the Query Bar.
Define operators and values
To filter the documents in the collection, define the required operators and values in the Query section and choose the fields you want to include in the Projection section.
Query section
The Query section allows you to add the search conditions for the query. For each condition, you specify the field name, the operator, and the field value.
By default, Visual Query Builder uses the $and
operator, Match all ($and) to combine multiple conditions where all the conditions must be satisfied for a match.
Select Match any ($or) to use the $or
operator to combine multiple conditions where any of the conditions can be true for a match.
Visual Query Builder automatically detects the value type, for example Int32, String, or RegEx from the fields that you drag and drop. You can change the data type by clicking on the type button and selecting the required data type from the list.
For string values, you can open a multiline text editor for editing long strings by clicking the ellipsis button.
Advanced editors are also available for binary, reference, and regex values.
Clone field rules
You can clone field rules using the duplicate button, for example when you need to specify fields with similar conditions.
Projection section
The Projection section allows you to include or exclude specific fields from the query results.
Sort section
The Sort section allows you to sort query results by a specific field, in either ascending or descending order.
Query MongoDB arrays
You can match on array elements using Visual Query Builder, either as a standalone query or as part of an AND/OR group.
Standalone query
To directly find array elements that match query criteria:
- In the Query section, choose Has array element(s) matching from the dropdown.
2. In the Array list, choose the array you want to query. We’ll look at the array cities_visited.
3. Step into the array-valued column you want to query.
4. Double-click in the box to select the default option <Array Element>
.
5. Select your operator (in this example, equals).
6. Type in the value to be matched on (in this example, Berlin).
Find array elements as part of an AND/OR group
1. Select one of the following operators:
- Match all ($and)
- Match any $or)
- Match none ($nor)
In this case, let’s use the $or
operator.
2. Click on Add AND/OR group.
3. From here, follow the steps in querying array elements in a standalone query.
4. Create more AND/OR groups as needed.
5. Run the query.
Run the MongoDB query
When you have the required fields, operators, and values in place, click the Run button (play symbol) at the top of the Collection tab or the Run button in the Visual Query Builder.
View the query in mongo shell language
Click on the Query Code tab to view the query in mongo shell language. MongoDB uses the find()
method to retrieve documents from a collection.
Generate JavaScript, Java, Python, PHP and C# code from built queries
In addition to showing the query in mongo shell language, Query Code can also translate any query you have run in Studio 3T into JavaScript (Node.js), Java (2.x, 3.x, and 4.x driver API), C#, Python, PHP, and Ruby.
To generate query code in other languages:
- Run the query
- Click on the Query Code tab
- Choose the target language