首页/数据/NoSQL 速查/$exists 与 $type 字段检查
本包目录(322 条)
MongoDB99 条
Elasticsearch89 条ClickHouse56 条时序与列式28 条选型与架构50 条

$exists 与 $type 字段检查

$exists 判断字段是否出现;$type 按 BSON 类型过滤,别名 number 覆盖 int/long/double/decimal,也可传类型数组。

NoSQL 速查数据更新于 2026-10-12
存在性{err: {$exists: true}}
类型{value: {$type: "string"}}
多类型$type: ["int", "double"]

示例

示例
db.logs.find({ err: { $exists: true } })
db.metrics.find({ value: { $type: ["int", "double"] } })
$type 与 $exists 多数情况下无法有效利用索引,大集合上开销明显。

易错点

  • 对数组字段按元素类型匹配:数组里含一个字符串元素也会被 $type:"string" 命中,解读结果时留意

数据来源与参考

  • MongoDB Manual: $type / $exists
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌