Query Your Data查询数据

You can type MongoDB filter documents into the query bar to display only documents which match the specified criteria. 您可以在查询栏中键入MongoDB筛选文档,以仅显示符合指定条件的文档。To learn more about querying documents, see Query Documents in the MongoDB manual.要了解有关查询文档的更多信息,请参阅MongoDB手册中的查询文档

  1. In the Filter field, enter a filter document. 在“筛选”字段中,输入筛选文档。You can use all of the MongoDB query operators except the $text and $expr operators.您可以使用除$text运算符和$expr运算符之外的所有MongoDB查询运算符

    Example

    The following filter only returns documents which have a Country value of Brazil:以下筛选器仅返回Country值为Brazil的文档:

    { Country: "Brazil" }
    
  2. Click Find to run the query and view the updated results.单击“查找”以运行查询并查看更新的结果。

    Results of applying a query filter
    click to enlarge点击放大
Note

For query result sets larger than 1000 documents, Compass shows a sampling of the results. 对于大于1000个文档的查询结果集,Compass显示结果的采样。 Otherwise, Compass shows the entire result set.否则,Compass将显示整个结果集。

For details on sampling, see Sampling.有关采样的详细信息,请参阅采样

The Compass Filter supports using the mongo shell mode representation of the MongoDB Extended JSON BSON data types.Compass筛选器支持使用MongoDB扩展JSON BSON数据类型的mongo shell模式表示。

Example示例

The following filter returns documents where start_date is greater than than the BSON Date 2017-05-01:以下筛选器返回start_date大于BSONDate 2017-05-01的文档:

{ "start_date": {$gt: new Date('2017-05-01')} }

By specifying the Date type in both start_date and the $gt comparison operator, Compass performs the greater than comparison chronologically, returning documents with start_date later than 2017-05-01.通过在start_date$gt比较运算符中指定Date类型,Compass按时间顺序执行greater than比较,返回开始日期晚于2017-05-01的文档。

Without the Date type specification, Compass compares the start_dates as strings lexicographically, instead of comparing the values chronologically.在没有Date类型规范的情况下,Compass将start_dates作为字符串按字典顺序进行比较,而不是按时间顺序进行比较。

To clear the query bar and the results of the query, click Reset.要清除查询栏和查询结果,请单击“重置”。

$filter corresponds to the WHERE clause in a SQL SELECT statement.$filter对应于SQL SELECT语句中的WHERE子句。

Example

You have 3,235 articles. You would like to see all articles that Joe Bloggs wrote.你有3235篇文章。你想看看乔·布洛格斯写的所有文章。

Compass Filter OptionCompass过滤器选项
{ author : { $eq : "Joe Bloggs" } }
MongoDB AggregationMongoDB聚合
db.article.aggregate(

  { $filter : { author : { $eq : "Joe Bloggs" } } }

);
SQL
SELECT * FROM article

WHERE author = "Joe Bloggs";
Example

The following examples use the JSON documents below as sample data. 以下示例使用下面的JSON文档作为示例数据。To import this sample data to your MongoDB deployment with MongoDB Compass:要使用MongoDB Compass将此示例数据导入MongoDB部署,请执行以下操作:

  1. Copy the array of documents below by clicking Copy.单击“复制”,复制下面的文档数组。
[

   {

      "name":"Andrea Le",

      "email":"andrea_le@fakegmail.com",

      "version":5,

      "scores":[85, 95, 75],

      "dateCreated":{"$date":"2003-03-26"}

   },

   {

      "email":"no_name@fakegmail.com",

      "version":4,

      "scores":[90, 90, 70],

      "dateCreated":{"$date":"2001-04-15"}

   },

   {

      "name":"Greg Powell",

      "email":"greg_powell@fakegmail.com",

      "version":1,

      "scores":[65, 75, 80],

      "dateCreated":{"$date":"1999-02-10"}

   }

]
  1. In Compass, use the left navigation panel to select the database and the collection you want to import the data to.在Compass中,使用左侧导航面板选择要将数据导入的数据库和集合。
  2. Click the Documents tab.单击“文档”选项卡。
  3. Click Add Data and select Insert Document.单击“添加数据”并选择“插入文档”。
  4. Ensure that View is set to JSON, or {}, and paste the copied JSON documents in the field.确保“视图”设置为JSON或{},并将复制的JSON文档粘贴到字段中。
  5. Click Insert.单击“插入”。
Note

If you do not have a MonogDB deployment or if you would like to query a large sample data set, see Sample Data for Atlas Clusters for instructions on creating a free-tier cluster with sample data. 如果您没有MonogDB部署,或者如果您想查询大型样本数据集,请参阅Atlas集群的样本数据,以获取有关使用样本数据创建自由层集群的说明。Note that the examples below are intended to filter the sample JSON documents provided on this page and may not properly filter another sample data set.请注意,下面的示例旨在过滤此页面上提供的示例JSON文档,可能无法正确过滤其他示例数据集。

For more query examples, see Query Documents in the MongoDB manual.有关更多查询示例,请参阅MongoDB手册中的查询文档