“文档” 菜单
文档首页
/
MongoDB Compass
/

查询数据

在此页面上

  • 兼容性
  • 设置查询筛选器
  • 举例
  • 按单个条件匹配
  • 按多个条件匹配 ($and)
  • 按多个可能条件匹配 ($or)
  • 按排除匹配 ($not)
  • 使用比较操作符进行匹配
  • 按日期匹配
  • 按数组条件匹配
  • 按子字符串匹配
  • 按嵌入式字段匹配
  • 查询栏支持的数据类型
  • 清除查询
  • 查询包含无效 UTF8 数据的集合
  • Compass 查询与 MongoDB 和 SQL 查询相比如何?

您可以在查询栏中输入 MongoDB 过滤器文档,仅显示符合指定标准的文档。要了解有关查询文档的详情,请参阅 MongoDB 手册中的查询文档

您可以对托管于以下环境中的部署进行数据进行查询:

  • MongoDB Atlas :用于在云中部署 MongoDB 的完全托管服务

  • MongoDB Enterprise:基于订阅、自行管理的 MongoDB 版本

  • MongoDB Community:source-available、免费使用且可自行管理的 MongoDB 版本

要了解有关查询 MongoDB Atlas 中托管部署的数据的更多信息,请参阅查找特定文档

  1. Filter(过滤器)字段中,在大括号之间输入过滤器文档。您可以使用除 $text$expr 操作符之外的所有 MongoDB 查询操作符

    例子

    以下筛选器返回 title 值为 Jurassic Park 的文档:

    { "title": "Jurassic Park" }
  2. 单击 Find(查找)运行查询,查看更新的结果。

    应用查询过滤器的结果
    点击放大

本页上的示例使用的是一个小型示例数据集。要将样本数据导入到 MongoDB 部署中,请执行以下步骤:

  1. 将以下文档复制到剪贴板:

    [
    {
    "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. 在 Compass,使用左侧导航面板 选择要将数据导入的数据库和集合。

  3. 单击 Documents 标签页。

  4. 单击 Add Data(添加数据)并选择 Insert Document(插入文档)。

  5. View(视图)设置为 JSON ({})。

  6. 将剪贴板中的 JSON 文档粘贴到模态窗口中。

  7. 单击 Insert(连接)。

注意

如果您没有 MongoDB 部署,或者想要查询更大的样本数据集,请参阅Atlas 集群的样本数据,了解使用样本数据创建免费套餐集群的说明。以下示例查询筛选此页面提供的示例文档。

以下查询筛选器查找 name 值为“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" }
}

以下查询筛选器查找 scores 数组包含值 75nameGreg 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 操作符查找 version4nameAndrea 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 操作符查找 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" }
}

以下查询过滤器查找 school.name 子字段为“Northwest”的文档:

{ "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 Filter 支持使用 MongoDB 扩展 JSON BSON 数据类型mongo shell 模式表示法。

例子

以下筛选器返回 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 作为字符串进行比较,而不是按时间顺序比较值。

要清除查询栏和查询结果,请单击 Reset

如果您尝试查询或导出包含无效 UTF8 字符的数据,则会显示以下错误消息:

Invalid UTF-8 string in BSON document.

要查询或导出该数据,请将 enableUtf8Validation URI 选项设置为 false 以禁用 UTF8 验证。

警告

如果编辑数据并设置 enableUtf8Validation=false,可能会导致数据丢失。该方法是仅查询或导出数据的临时解决方法。

以下 URI 禁用 UTF8 验证:

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

注意

您也可以在 Advanced Connection Options (高级连接选项)中禁用此选项,方法是选择enableUtf8Validation并输入false

$filter 对应于 SQL SELECT 语句中的 WHERE 子句。

例子

现有 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";

后退

删除多个文档

来年

设置返回的字段