Visual Query Builder图形化查询生成器

Posted on: 19/11/2018 (last updated: 09/03/2022) by Dakota Karlsson
tl;dr

Visual Query Builder lets you 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. 图形化查询生成器允许您通过拖放字段以可视化方式构建查询,这是一项非常适合那些不知道或仍在学习MongoDB查询语法的人的功能。Try it today.

Shortcuts快捷键

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切换到JSON视图Ctrl + Alt + 3 ( + + 3)

Visual Query Builder is just one of four ways to query MongoDB in Studio 3T.“图形化查询生成器”只是Studio 3T中查询MongoDB的四种方法之一

Basics基础

The Visual Query Builder allows for the creation of MongoDB queries via drag-and-drop, in combination with the Table and Tree Views.“图形化查询生成器”允许通过拖放,结合表和树视图创建MongoDB查询。

https://player.vimeo.com/video/681881851

Download Studio 3T in case you haven’t yet to start using Visual Query Builder.如果尚未开始使用图形化查询生成器,请先下载Studio 3T

Studio 3T’s drag-and-drop MongoDB query builder is part of the greater Collection Tab, which is where users can view, query, and edit documents within the collection.Studio 3T的拖放MongoDB查询生成器是更大的“集合”选项卡的一部分,用户可以在该选项卡中查看、查询和编辑集合中的文档。

The Collection Tab houses both the Visual Query Builder:“集合”选项卡包含两个图形化查询生成器:

And the Main Query Bar, which shows the mongo shell’s syntax as it is being built:以及主查询栏,它在构建mongo shell时显示其语法:

There are three ways to open the Visual Query Builder:有三种方法可以打开“图形化查询生成器”:

Prefer the mongo shell? 喜欢mongo shell吗?Autocomplete queries with IntellIShell instead.改为使用IntellIShell自动完成查询。

Build the MongoDB query构建MongoDB查询

Let’s build a query using Studio 3T’s MongoDB query builder and the Customers replica data set, which can be downloaded here.让我们使用Studio 3T的MongoDB查询生成器和Customers副本数据集构建一个查询,可以在这里下载。

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.要执行此操作,请按住ctrl键并单击要使用的字段。

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:以下是使用上述GIF中的“图形化查询生成器”以MongoDB查询语法生成的查询:

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.要进一步筛选查询,请在“查询”、“投影”和“排序”部分中定义所需的“运算符”和“值”。

Query section“查询”部分

State the operators as needed under the Query Section.根据需要在“查询”部分下说明运算符。

See the full list of MongoDB Query and Projection operators here.请参见此处的MongoDB查询和投影运算符的完整列表。

By default, the $and operator is automatically used to combine queries.默认情况下,$and运算符自动用于组合查询。

The $or operator is NOT selected by default and must be manually added. 默认情况下不选择$or运算符,必须手动添加。To ensure functionality, state the $or operator before defining the fields as seen in the example below.为了确保功能性,请在定义字段之前声明$or运算符,如下面的示例所示。

The Visual Query Builder will automatically detect value type (e.g. Int32, string, regex) from added fields. “图形化查询生成器”将从添加的字段中自动检测值类型(例如Int32stringregex)。Switch between field types by clicking on the Value icon.通过单击值图标在字段类型之间切换。

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.对于正则表达式、未定义和二进制值,也可以找到类似的高级编辑器。

Click here for the complete list of MongoDB-supported BSON types.单击此处查看MongoDB支持的BSON类型的完整列表。

Projection section“投影”部分

The Projection section allows users to include or exclude specific fields from the query results.“投影”部分允许用户从查询结果中包括或排除特定字段。

Sort section“排序”部分

The Sort section lets users sort query results by a specific field, in either ascending or descending order.“排序”部分允许用户按特定字段按升序或降序对查询结果进行排序。

It’s important to note that the Sort section can only define one field at a time.需要注意的是,“排序”部分一次只能定义一个字段。

Read more about how MongoDB can sort data.阅读更多关于MongoDB如何对数据进行排序的信息。

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.2018.5.1版中,您现在还可以一键克隆字段规则,这是一个很好的功能,可以避免重复拖放,尤其是在处理非常相似的字段规则时。

Query MongoDB arrays查询MongoDB数组

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.“查询”部分中,从下拉列表中选择“具有匹配的数组元素”。

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).我们将看看你访问的数组cities_visited,它最多包含两个元素(见列)。

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. 对于VQB的拖动功能的老手来说,这似乎是不自然的。Dragging array elements will cause Studio 3T to automatically take the JSON path (e.g. cities_visited_0). 拖动数组元素将导致Studio 3T自动采用JSON路径(例如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).定义运算符(如equalscontains)。

6. Drag or type in the value to be matched on (in this example, Berlin).拖动或键入要匹配的值(在本例中为Berlin)。

7. Drag or type in the value to be matched on (in this example, Berlin). 拖动或键入要匹配的值(在本例中为Berlin)。

Find array elements as part of an AND/OR group将数组元素作为AND/OR组的一部分查找

Querying in an AND/OR group follows the same steps as a standalone query.在AND/OR组中进行查询的步骤与独立查询相同。

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)选择以下运算符之一:-匹配所有($and
    • Match any of ($or)匹配任何($or
    • Match none of ($nor)In this case, there’s take the $or operator.不匹配($nor)在这种情况下,需要使用$or运算符。

2. Click on Add AND/OR group.点击添加AND/OR分组。

3. From here, follow steps 1-6 of querying array elements in a standalone query.从这里开始,按照在独立查询中查询数组元素的步骤1-6进行操作。

4. Create more AND/OR groups as needed.根据需要创建更多和/或组。

5. Run the query.运行查询。

View the built query in the mongo shell language用mongo shell语言查看构建的查询

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.“图形化查询生成器”在主查询栏下以“Mongo Shell语法”同时显示生成的查询,这也显示了在生成查询时对其所做的更改。

The Query Code tab also shows the built query in JSON and four other languages.“查询代码”选项卡还显示了用JSON和其他四种语言构建的查询。

Run the MongoDB query运行MongoDB查询

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.在正确的字段、值和运算符就位后,单击屏幕顶部的“执行”按钮(播放符号)或屏幕底部的“运行”按钮。

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.要首先显示最新的文档,请按字段_id降序排列MongoDB集合,或按升序排列以首先显示最旧的文档。

Generate JavaScript, Java, Python, PHP and C# code from built queries从内置查询生成JavaScript、Java、Python、PHP和C#代码

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#.除了以完整的mongo shell语法显示构建的查询外,查询代码还将构建的查询转换为JavaScript(Node.js)、Java(2.x和3.x驱动程序API)、Python和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“选择”目标语言

New to MongoDB? Be productive from day one with these helpful features:MongoDB新手?从第一天开始,通过以下有用的功能提高工作效率: