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以下筛选器仅返回title
value ofJurassic Park
:title
值为Jurassic Park
的文档:{ "title": "Jurassic Park" }
Click Find to run the query and view the updated results.单击“查找”运行查询并查看更新的结果。
Supported Data Types in the Query Bar查询栏中支持的数据类型
The Compass Filter supports using the Compass“筛选器”支持使用MongoDB Extended 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按时间顺序执行大于比较,返回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
子句。
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 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 以下查询筛选器查找名称值为“Andrea Le”的所有文档。name
is "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 thename
field is not equal to "Andrea Le", orname
字段的值不等于“Andrea Le”,或者 -
Thename
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"}
}
]
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"}
}
]
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手册中的查询文档。