Visual Query Builder

Visual Query Builder lets users build queries visually by dragging and dropping fields, a feature perfect for those who do not know or are still learning the MongoDB query syntax.

Open Visual Query Builder - Ctrl + F (⌘+ F)
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

The Visual Query Builder allows for the creation of MongoDB queries via drag-and-drop, in combination with the Table and Tree Views.

Download Studio 3T in case you haven’t yet to start using Visual Query Builder.

Studio 3T’s drag-and-drop MongoDB query builder is part of the greater Collections Tab, which is where users can view, query, and edit documents within the collection.

The Collections Tab houses the Visual Query Builder in Studio 3T.

The Collections Tab houses both the Visual Query Builder:

The Visual Query Builder has 3 sections: Query, Projection, and Sort. Use drag-and-drop documents to start building a MongoDB query.

And the Main Query Bar, which shows the mongo shell’s syntax as it is being built:

The Main Query Bar will reflect the query being built in the Visual Query Builder. You can also edit your query via here as well.

There are three ways to open the Visual Query Builder:

Button

Click the Query Builder button

The simplest way to open the Visual Query Builder is to click the 'Visual Query Builder'.

Right-click

Right-click anywhere in the Result tab and choose Query Builder

Hotkey

Press Ctrl + F (⌘+ F)

Prefer the mongo shell? Autocomplete queries with IntellIShell instead.

Build the MongoDB query

Let’s build a query using Studio 3T’s MongoDB query builder and the Customers replica data set, which can be downloaded here.

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 the Query Builder.

In Table View, fields can only be selected one at a time.

In Tree View, multiple lines can be selected and dropped into the Visual Query Builder simultaneously. To do this, hold ‘ctrl‘ and click the fields to be used.

Customize the query

To better specify the query, drag as many fields as necessary.

Here is the query built using the Visual Query Builder from the above GIF, in MongoDB query syntax:

db.Customers.find(
{
"title" : "Ms",
"transactions" : {
"$gt" : NumberInt(46)
}
}
);

Define values and operators

To further filter queries, define the desired operators and values in the Query, Projection, and Sort sections.

Build your query in the in Query section, decide what fields to show in the Projection section, and choose how the results are sorted in the Sort section.

Query section

State the operators as needed under the Query Section.

The MongoDB operators available.

By default, the $and operator is automatically used to combine queries.

The $or operator is NOT selected by default and must be manually added. To ensure functionality, state the $or operator before defining the fields as seen in the example below.

The Visual Query Builder will automatically detect value type (e.g. Int32, string, regex) from added fields. Switch between field types by clicking on the Value icon.

Supported BSON value types in Studio 3T.

For string values, a multiline text editor – handy for editing long strings – can be opened by clicking on the ellipsis icon.

Similar advanced editors can also be found for regex, undefined, and binary values.

Multi-line Text Editor.

Click here for the complete list of MongoDB-supported BSON types.

Projection section

The Projection section allows users to include or exclude specific fields from the query results.

The Projection section in Studio 3T. Use this to specify how results are presented.

Sort section

The Sort section lets users sort query results by a specific field, in either ascending or descending order.

If multiple fields are defined (say, last name – ascending and first name – descending), Visual Query Builder will sort the results by the first field (last name). The second field (first name) won’t be considered.

Choose how results are sorted and in what order via the Sort section.

Read more about how MongoDB can sort data.

Clone field rules

With Release 2018.5.1, you can now also clone field rules in one click – a nifty feature that spares you the repetitive drag-and-drop, especially when you’re dealing with very similar field rules.

Query MongoDB arrays

It is also possible to match on array elements using the Visual Query Builder, either as a standalone query or as part of an AND/OR group.

We highly recommend Table View when querying array elements, so that you can easily step into and out of array-valued columns.

Standalone query

To directly find array elements that match a query criteria:

  1. Within the Query Section, choose Has array element(s) matching from the dropdown.
    Choose Has array elements matching from the dropdown
  2. This will trigger the Array dropdown to appear. Choose the array you want to query. We will look at the array cities_visited, which contains two elements at most (see column).
    Choose the array you want to query from the dropdown
  3. Step into the array-valued column you want to query.
  4. Double-click in the Query Section to choose the default option <Array Element>.

    This might seem unnatural to veterans of VQB’s dragging function. Dragging array elements will cause Studio 3T to automatically take the JSON path (e.g. cities_visited_0). For the query to work, you need to set the field to the default option <Array Element>.

  5. Define your operator (e.g. equals, contains)
  6. Drag or type in the value to be matched on (in this example, Berlin).
  7. Run the query.

Find array elements as part of an AND/OR group

Querying in an AND/OR group follows the same steps as a standalone query.

The only difference is, you must first state the desired operator at the top of the Query Section.

  1. Select one of the following operators:- Match all of ($and)
    – Match any of ($or)
    – Match none of ($nor)In this case, there’s take the $or operator.
  2. Click on Add AND/OR group.Click on Add AND/OR group
  3. From here, follow steps 1-6 of querying array elements in a standalone query.
  4. Create more AND/OR groups as needed.AND/OR MongoDB array elements
  5. Run the query.

View the built query in the mongo shell language

The Visual Query Builder simultaneously displays the built query in mongo shell syntax under the Main Query Bar, which also shows the changes made to it as it’s being built.

View your query in the mongo Shell language via the Main Query Bar.

The Query Code tab also shows the built query in JSON and four other languages.

Run the MongoDB query

Once the correct fields, values, and operators are in place, click the Execute button (play symbol) at the top of the screen or the Run button towards the bottom of the screen.

Run the query you;ve built in teh Visual Query Builder.

Order documents by creation date

To display the newest documents first, sort the MongoDB collection by the field _id in descending order, or in ascending order to show oldest documents first.

Generate JavaScript, Java, Python, PHP and C# code from built queries

In addition to showing the built query in its full mongo shell syntax, Query Code also translates built queries into JavaScript (Node.js), Java (2.x and 3.x driver API), Python and C#.

To see a built query’s equivalent code:

  1. Execute the query
  2. Click on the Query Code tab
  3. Choose the target language

Translate your query from the mongo Shell language into: JavaScript, Java, C#, Python, and PHP.


New to MongoDB? Be productive from day one with these helpful features:

  • SQL Query – Use SQL to query MongoDB and see how it translates to the MongoDB query language
  • Aggregation Editor – Write MongoDB aggregation queries in stages
  • Import/Export Wizard – Import/export in various formats, without writing a single command

Updated on November 21, 2018

Article Attachments

Was this article helpful?

Related Articles