Docs Home / Compass / Interact with Your Data

Query Your Data查询数据

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

Compatibility兼容性

You can query your data for deployments hosted in the following environments:您可以查询在以下环境中托管的部署的数据:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

To learn more about querying your data for deployments hosted in MongoDB Atlas, see Find Specific Documents.要了解有关查询MongoDB Atlas中托管部署的数据的更多信息,请参阅查找特定文档

Set Query Filter设置查询筛选器

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

    Example示例

    The following filter returns documents that 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

Large Integer Limitations大整数限制

To query for integers larger than the JavaScript maximum integer size, wrap the integer in string characters and pass the integer to the NumberLong() constructor.要查询大于JavaScript最大整数大小的整数,请将整数包装为字符串字符,并将整数传递给NumberLong()构造函数。

For example, to find all documents with a number value of 507550989629521900, use this query filter:例如,要查找number值为507550989629521900的所有文档,请使用此查询筛选器:

{ "number" : NumberLong("507550989629521900") }

Examples示例

The examples on this page use a small example dataset. To import the sample data into your MongoDB deployment, perform the following steps:此页面上的示例使用了一个小的示例数据集。要将示例数据导入MongoDB部署,请执行以下步骤:

  1. Copy the following documents to your clipboard:将以下文档复制到剪贴板:

    [
    {
    "name": "Andrea Le",
    "email": "andrea_le@fake-mail.com",
    "school": {
    "name": "Northwestern"
    },
    "version": 5,
    "scores": [ 85, 95, 75 ],
    "dateCreated": { "$date": "2003-03-26" }
    },
    {
    "email": "no_name@fake-mail.com",
    "version": 4,
    "scores": [ 90, 90, 70 ],
    "dateCreated": { "$date": "2001-04-15" }
    },
    {
    "name": "Greg Powell",
    "email": "greg_powell@fake-mail.com",
    "version": 1,
    "scores": [ 65, 75, 80 ],
    "dateCreated": { "$date": "1999-02-10" }
    }
    ]
  2. In Compass, use the left navigation panel to select the database and the collection you want to import the data to.在Compass中,使用左侧导航面板选择要导入数据的数据库和集合。
  3. Click the Documents tab.单击“文档”选项卡。
  4. Click Add Data and select Insert Document.单击“添加数据”,然后选择“插入文档”。
  5. Set the View to JSON ({}).将“视图”设置为JSON({})。
  6. Paste the JSON documents from your clipboard into the modal.将剪贴板中的JSON文档粘贴到模态中。
  7. Click Insert.单击“插入”。

Note

If you do not have a MongoDB deployment or if you want to query a larger sample data set, see Sample Data for Atlas Clusters for instructions on creating a free-tier cluster with sample data. The following example queries filter the sample documents provided on this page.如果您没有MongoDB部署,或者您想查询更大的示例数据集,请参阅Atlas Clusters的示例数据,了解使用示例数据创建自由层集群的说明。以下示例查询筛选了此页面上提供的示例文档。

Match by a Single Condition按单一条件匹配

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

{ name: "Andrea Le" }

The query returns the following document:查询返回以下文档:

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

Match by Multiple Conditions ($and)按多个条件匹配($and

The following query filter finds all documents where scores array contains the value 75, and the name is Greg Powell:以下查询筛选器查找所有scores(分数)数组包含值75nameGreg Powell的文档:

{ $and: [ { scores: 75, name: "Greg Powell" } ] }

The query returns the following document:查询返回以下文档:

{
"_id": { "$oid":"5a9427648b0beebeb69579cf" },
"name": "Greg Powell",
"email": "greg_powell@fake-mail.com",
"version": 1,
"scores": [ 65, 75, 80 ],
"dateCreated": { "$date": "1999-02-10" }
}

Match by Multiple Possible Conditions ($or)按多种可能条件匹配($or

The following query filter uses the $or operator to find documents where version is 4, or name is Andrea Le:以下查询筛选器使用$or运算符查找version(版本)为4name"Andrea Le"的文档:

{ $or: [ { version: 4 }, { name: "Andrea Le" } ] }

The query returns the following documents:查询返回以下文档:

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

Match by Exclusion ($not)排除匹配($not

The following query filter uses the $not operator to find all documents where the value of the name field is not equal to "Andrea Le", or the name field does not exist:以下查询筛选器使用$not运算符查找name字段值不等于"Andrea Le"name字段不存在的所有文档:

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

The query returns the following documents:查询返回以下文档:

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

Tip

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

Match with Comparison Operators与比较运算符匹配

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

{ version: { $lte: 4 } }

The query returns the following documents:查询返回以下文档:

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

Tip

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

Match by Date按日期匹配

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: new Date('2000-06-22') } }

The query returns the following documents:查询返回以下文档:

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

Match by Array Conditions按数组条件匹配

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:查询返回以下文档,因为scores(分数)数组中的一个值是85

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

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

Match by Substring按子字符串匹配

The following query filter uses the $regex operator to find all documents where the value of email includes the term "andrea_le":以下查询筛选器使用$regex运算符查找email值包含"andrea_le"一词的所有文档:

{ email: { $regex: "andrea_le" } }

The query returns the following document:查询返回以下文档:

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

Match by Embedded Field按嵌入字段匹配

The following query filter finds the document with the school.name subfield of :以下查询筛选器查找school.name(学校名称)子字段为"Northwestern"的文档:

{ "school.name": "Northwestern" }

The query returns the following document:

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

For more query examples, see Query Documents in the MongoDB manual.

Match by Specific Field and Value按特定字段和值匹配

If a document contains a specific field and value that you want your query to match, you can right-click on the desired field and select Add to query from the dropdown:如果文档包含您希望查询匹配的特定字段和值,您可以右键单击所需字段,然后从下拉列表中选择“添加到查询”:

Right click to add field to query

This adds a clause that matches the field and its specific value to the query bar. In the preceding example, Compass adds a query clause that matches { runtime: 72 }.这将向查询栏添加一个与字段及其特定值匹配的子句。在前面的示例中,Compass添加了一个与{runtime:72}匹配的查询子句。

If you have not defined a query, Compass creates one in the query bar.如果您尚未定义查询,Compass会在查询栏中创建一个查询。

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

The Compass Filter supports using the mongosh representation of the MongoDB Extended JSON BSON data types.Compass Filter支持使用MongoDB扩展JSON BSON数据类型的mongosh表示。

Example示例

The following filter returns documents where start_date is greater than than the BSON Date 2017-05-01:以下筛选器返回start_date大于BSON日期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.要清除查询栏和查询结果,请单击“重置”。

Query Collections with Invalid UTF8 Data查询包含无效UTF8数据的集合

If you attempt to query or export data with invalid UTF8 characters the following error message displays:如果您尝试查询或导出包含无效UTF8字符的数据,将显示以下错误消息:

Invalid UTF-8 string in BSON document.

To query or export this data, disable UTF8 validation by setting the enableUtf8Validation URI option to false.要查询或导出此数据,请通过将enableUtf8Validation URI选项设置为false来禁用UTF8验证。

Warning

Editing data with enableUtf8Validation=false can result in loss of data. This approach is a temporary workaround to query or export data only.使用enableUtf8Validation=false编辑数据可能会导致数据丢失。此方法是仅查询或导出数据的临时解决方法。

The following URI disables UTF8 validation:以下URI禁用UTF8验证:

mongodb://localhost:27017/?enableUtf8Validation=false

Note

You can also disable this option in the Advanced Connection Options by selecting enableUtf8Validation and entering false.您还可以在“高级连接选项”中禁用此选项,方法是选择enableUtf8Validation并输入false

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( { $match: { "author": "Joe Bloggs" } } )
SQL
SELECT * FROM article
WHERE author = "Joe Bloggs";