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 JSON 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 is a feature available in all versions of Studio 3T. It allows for the creation of MongoDB queries via drag-and-drop, in combination with the Table and Tree Views.

 

The Visual 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 both the Visual Query Builder:

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

The Main Query Bar.

There are three ways to open the Visual Query Builder:

Button

Click the Query Builder button

The Visual Query Button.

Right-click

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

Hotkey

Press Ctrl + F (⌘+ F)

Build the query

Let’s build a query using the Visual 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 be selected one at a time. However 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.

Drag and drop multiple documents with Tree View.

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 JSON query syntax:

db.Customers.find(
{
"title" : "Mr",
"package" : "XL",
"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.

The sections in the Visual Query Builder

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.

BSON types.

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.

VQB Projection Pane.

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.

VQB Sorting Pane.

Read more about how MongoDB can sort data.

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.

Main Query Bar.

The original query has now been extended as follows:

Original query


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

Final query

db.Customers.find(
{
"title" : "Mr",
"package" : "XL",
"transactions" : {
"$gt" : NumberInt(46)
}
},
{
"title" : 1.0,
"first" : 1.0,
"last" : 1.0,
"package" : 1.0,
"transactions" : 1.0
}
).sort(
{
"last" : 1.0
}
);

Run the 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 Query symbols.

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.

Updated on June 22, 2018

Was this article helpful?

Related Articles