Query Your Data查询数据¶
On this page
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设置查询筛选器¶
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查询运算符。ExampleThe following filter only returns documents which have a以下筛选器仅返回Country
value ofBrazil
:Country
值为Brazil
的文档:{ Country: "Brazil" }
-
Click Find to run the query and view the updated results.单击“查找”以运行查询并查看更新的结果。click to enlarge点击放大
Supported Data Types in the Query Bar查询栏中支持的数据类型¶
The Compass Filter supports using the Compass筛选器支持使用MongoDB扩展JSON BSON数据类型的mongo
shell mode representation of the MongoDB Extended JSON BSON data types.mongo
shell模式表示。
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
作为字符串按字典顺序进行比较,而不是按时间顺序进行比较。
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
子句。
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示例¶
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部署,请执行以下操作:
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"}
}
]
In Compass, use the left navigation panel to select the database and the collection you want to import the data to.在Compass中,使用左侧导航面板选择要将数据导入的数据库和集合。Click the Documents tab.单击“文档”选项卡。Click Add Data and select Insert Document.单击“添加数据”并选择“插入文档”。Ensure that View is set to JSON, or确保“视图”设置为JSON或{}
, and paste the copied JSON documents in the field.{}
,并将复制的JSON文档粘贴到字段中。Click Insert.单击“插入”。
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手册中的查询文档。