| {field: null} | 匹配 null 值或字段缺失 |
|---|---|
| {field: {$type: "null"}} | 仅显式存储的 null |
| {field: {$exists: true, $ne: null}} | 存在且不为 null |
示例
示例
db.users.find({ phone: null })
db.users.find({ phone: { $exists: true, $ne: null } })普通索引会把 null 值一起编入索引,用索引优化“查空值”时要结合索引形态分析。
{field: null} 同时匹配“值为 null”和“字段缺失”的文档;只想要显式 null 用 $type:"null",只想要存在且非空用 $exists+$ne 组合。
| {field: null} | 匹配 null 值或字段缺失 |
|---|---|
| {field: {$type: "null"}} | 仅显式存储的 null |
| {field: {$exists: true, $ne: null}} | 存在且不为 null |
db.users.find({ phone: null })
db.users.find({ phone: { $exists: true, $ne: null } })