查询您的数据
本页内容
您可以在查询栏中键入 MongoDB 过滤文档,以仅显示符合指定条件的文档。有关查询文档的更多信息,请参阅查询文档(MongoDB 手册)。
兼容性
您可以使用查询您的数据以下环境托管的部署
MongoDB Atlas:云中MongoDB部署的全托管服务
MongoDB Enterprise:基于订阅的自托管MongoDB版本
MongoDB Community:源代码可用的、免费使用并自托管的MongoDB版本
了解更多关于查询您的数据在MongoDB Atlas中的部署,请参阅 查找特定文档。
设置查询过滤器
示例
本页上的示例使用一个小示例数据集。要将样本数据导入您的 MongoDB 部署中,请执行以下步骤
将以下文档复制到您的剪贴板
[ { "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" } } ] 在Compass中,使用左侧导航面板选择要导入数据的数据库和集合。
点击 文档 选项卡。
点击 添加数据 并选择 插入文档。
将 视图 设置为 JSON (
{}
)。将剪贴板中的 JSON 文档粘贴到模态窗口中。
点击 插入。
注意
如果您没有 MongoDB 部署或想要查询更大的样本数据集,请参阅 Atlas 集群的样本数据 以获取创建具有样本数据的免费层集群的说明。以下示例查询筛选了本页上提供的样本文档。
按单个条件匹配
以下查询过滤器查找所有名称为"Andrea Le"的文档
{ name: "Andrea Le" }
查询返回以下文档
{ "_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" } }
按多个条件匹配($and)
以下查询过滤器查找所有"scores"数组包含值75,并且名称为"Greg Powell"的文档
{ $and: [ { scores: 75, name: "Greg Powell" } ] }
查询返回以下文档
{ "_id": { "$oid":"5a9427648b0beebeb69579cf" }, "name": "Greg Powell", "email": "greg_powell@fake-mail.com", "version": 1, "scores": [ 65, 75, 80 ], "dateCreated": { "$date": "1999-02-10" } }
按多个可能条件匹配($or)
以下查询过滤器使用$or
运算符查找版本为4或名称为"Andrea Le"的文档
{ $or: [ { version: 4 }, { name: "Andrea Le" } ] }
查询返回以下文档
[ { "_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" } } ]
通过排除匹配($not)
以下查询过滤器使用$not
运算符来查找所有name
字段的值不等于"Andrea Le",或者name
字段不存在
{ name: { $not: { $eq: "Andrea Le" } } }
查询返回以下文档
[ { "_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" } } ]
使用比较运算符进行匹配
以下查询过滤器使用$lte
运算符来查找所有version
小于或等于4
{ version: { $lte: 4 } }
查询返回以下文档
[ { "_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" } } ]
按日期匹配
以下查询过滤器使用了 $gt
操作符和 Date()
方法,以查找所有 dateCreated
字段值晚于2000年6月22日的文档
{ dateCreated: { $gt: new Date('2000-06-22') } }
查询返回以下文档
[ { "_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" } } ]
按数组条件匹配
以下查询过滤器使用了 $elemMatch
操作符来查找所有在 scores
数组中至少有一个值大于 80
且小于 90
的文档
{ scores: { $elemMatch: { $gt: 80, $lt: 90 } } }
查询返回以下文档,因为 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" } }
有关更多查询示例,请参阅 MongoDB 手册中的 查询文档。
按子字符串匹配
以下查询过滤器使用了 $regex
操作符来查找所有 email
的值包含 "andrea_le" 术语的文档
{ email: { $regex: "andrea_le" } }
查询返回以下文档
{ "_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" } }
通过内嵌字段匹配
以下查询过滤器查找具有 "Northwestern" 子字段的 school.name
文档
{ "school.name": "Northwestern" }
查询返回以下文档
{ "_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" } }
有关更多查询示例,请参阅 MongoDB 手册中的 查询文档。
查询栏支持的数据类型
Compass 过滤器支持使用 MongoDB 扩展 JSON 的 mongosh
表示形式,用于表示 BSON 数据类型。
示例
以下过滤器返回 start_date
大于 BSON Date
2017-05-01
的文档
{ "start_date": {$gt: new Date('2017-05-01')} }
通过在 start_date
和 $gt
比较运算符中都指定 Date
类型,Compass 按时间顺序执行 greater than
比较操作,返回 start_date
晚于 2017-05-01
的文档。
如果没有指定 Date
类型,Compass 将将 start_dates
作为字符串 字典序 进行比较,而不是按时间顺序比较值。
清除查询
要清除查询栏和查询结果,点击 重置。
查询包含无效UTF8数据的集合
如果您尝试查询或导出包含无效UTF8字符的数据,将显示以下错误消息
Invalid UTF-8 string in BSON document.
要查询或导出这些数据,请通过将 enableUtf8Validation
URI 选项设置为 false
来禁用UTF8验证。
警告
编辑数据 时 enableUtf8Validation=false
可能会导致数据丢失。这种方法是查询或导出数据的临时解决方案。
以下URI禁用了UTF8验证
mongodb://localhost:27017/?enableUtf8Validation=false
注意
您也可以在高级连接选项中禁用此选项,通过选择enableUtf8Validation并输入false
。
Compass查询与MongoDB和SQL查询相比如何?
$filter
对应于SQL SELECT
语句中的WHERE
子句。SQL SELECT
语句。
示例
您有3,235篇文章。您想查看Joe Bloggs写的所有文章。
- Compass过滤器选项
{ author : { $eq : "Joe Bloggs" } } - MongoDB聚合
db.article.aggregate( { $match: { "author": "Joe Bloggs" } } ) - SQL
SELECT * FROM article WHERE author = "Joe Bloggs";