Docs HomeMongoDB Compass

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手册中的查询文档

Set Query Filter设置查询筛选器

  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 title value of Jurassic Park:以下筛选器仅返回title值为Jurassic Park的文档:

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

    Results of applying a query filter
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.有关采样的详细信息,请参阅采样

Supported Data Types in the Query Bar查询栏中支持的数据类型

The Compass Filter supports using the mongo shell mode representation of the MongoDB Extended JSON BSON data types.Compass“筛选器”支持使用MongoDB Extended 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按时间顺序执行大于比较,返回start_Date晚于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作为字符串进行比较,而不是按时间顺序比较值。

Clear the Query清除查询

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

How Does the Compass Query Compare to MongoDB and SQL Queries?Compass查询与MongoDB和SQL查询相比如何?

$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";

Examples示例

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 MongoDB 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. 如果您没有MongoDB部署,或者想要查询大型样本数据集,请参阅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文档,可能无法正确筛选其他示例数据集。

The following query filter finds all documents where the value of name is "Andrea Le".以下查询筛选器查找名称值为“Andrea Le”的所有文档。

{ name: "Andrea Le" }

The query returns the following document because the name field value is an exact match.查询返回以下文档,因为name字段值完全匹配。

{
"_id": {"$oid": "5e349915cebae490877d561d"},
"name":"Andrea Le",
"email":"andrea_le@fakegmail.com",
"version":5,
"scores":[85, 95, 75],
"dateCreated":{"$date":"2003-03-26"}
}

The following query filter uses the $not operator to find all documents where:以下查询筛选器使用$not运算符查找所有文档,其中:

  • The value of the name field is not equal to "Andrea Le", orname字段的值不等于“Andrea Le”,或者

  • The name field does not existname字段不存在

{ name: { $not: { $eq: "Andrea Le" } } }

The query returns the following documents because the name field either does not exist or its value is something other than "Andrea Le".查询返回以下文档,因为name字段不存在,或者其值不是“Andrea Le”。

[
{
"_id":{"$oid":"5e349915cebae490877d561e"},
"email":"no_name@fakegmail.com",
"version":4,
"scores":[90, 90, 75],
"dateCreated":{"$date":"2001-04-15"}
},
{
"_id":{"$oid":"5a9427648b0beebeb69579cf"},
"name":"Greg Powell",
"email":"greg_powell@fakegmail.com",
"version":1,
"scores":[65, 75, 80],
"dateCreated":{"$date":"1999-02-10"}
}
]
Tip

See also: 另请参见:

For a complete list of logical query operators, see Logical Query Operators.有关逻辑查询运算符的完整列表,请参阅逻辑查询运算符

The following query filter uses the $lte operator to find all documents where version is less than or equal to 4.以下查询筛选器使用$lte运算符查找版本小于或等于4的所有文档。

{ version: { $lte: 4 } }

The query returns the following documents because the version field values are less than or equal to 4.查询返回以下文档,因为version字段值小于或等于4

[
{
"_id":{"$oid":"5e349915cebae490877d561e"},
"email":"no_name@fakegmail.com",
"version":4,
"scores":[90, 90, 75],
"dateCreated":{"$date":"2001-04-15"}
},
{
"_id":{"$oid":"5a9427648b0beebeb69579cf"},
"name":"Greg Powell",
"email":"greg_powell@fakegmail.com",
"version":1,
"scores":[65, 75, 80],
"dateCreated":{"$date":"1999-02-10"}
}
]
Tip

See also:

For a complete list of comparison operators, see Comparison Query Operators.有关比较运算符的完整列表,请参阅比较查询运算符

The following query filter uses the $gt operator and Date() method to find all documents where the dateCreated field value is later than June 22nd, 2000.以下查询筛选器使用$gt运算符Date()方法查找dateCreated字段值晚于2000年6月22日的所有文档。

{ dateCreated: { $gt: Date('2000-06-22') } }

The query returns the following documents because the dateCreated field values are after June 22, 2000.查询返回以下文档,因为dateCreated字段值在2000年6月22日之后。

[
{
"_id": {"$oid": "5e349915cebae490877d561d"},
"name":"Andrea Le",
"email":"andrea_le@fakegmail.com",
"version":5,
"scores":[85, 95, 75],
"dateCreated":{"$date":"2003-03-26"}
},
{
"_id":{"$oid":"5e349915cebae490877d561e"},
"email":"no_name@fakegmail.com",
"version":4,
"scores":[90, 90, 75],
"dateCreated":{"$date":"2001-04-15"}
}
]

The following query filter uses the $elemMatch operator to find all documents where at least one value in the scores array is greater than 80 and less than 90以下查询筛选器使用$elemMatch运算符查找scores数组中至少有一个值大于80且小于90的所有文档

{ scores: { $elemMatch: { $gt: 80, $lt: 90 } } }

The query returns the following document because one of the values in the scores array is 85, which matches the $elemMatch criteria.查询返回以下文档,因为分数数组中的一个值是85,它与$elemMatch标准匹配。

{
"_id": {"$oid": "5e349915cebae490877d561d"},
"name":"Andrea Le",
"email":"andrea_le@fakegmail.com",
"version":5,
"scores":[85, 95, 75],
"dateCreated":{"$date":"2003-03-26"}
}

The following query filter finds all documents where the UUID is "002636e1-10cd-4c8b-a9a7-01b7bfd3899c".以下查询筛选器查找UUID为“002636e1-10cd-4c8b-a9a7-01b7bfd38999c”的所有文档。

{_id: UUID('002636e1-10cd-4c8b-a9a7-01b7bfd3899c')}

The query returns the following document because the UUID is an exact match.查询返回以下文档,因为UUID完全匹配。

{
"_id": UUID('002636e1-10cd-4c8b-a9a7-01b7bfd3899c'),
"name":"Mr. Florencio Breitenberg",
"email":"Florencio.Breitenberg",
"phone":"876.349.6791 x616"
"website":"nelle.name",
"country": "Zimbabwe",
"age":31
}

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