我有一个Email
文档有一个sent_at
日期字段:
{
'sent_at': Date( 1336776254000 )
}
If this Email
尚未发送,则sent_at
字段要么为空,要么不存在。
我需要获取所有已发送/未发送的计数Emails
。我一直在试图找出查询此信息的正确方法。我认为这是获取发送计数的正确方法:
db.emails.count({sent_at: {$ne: null}})
但是我应该如何获取未发送的数量呢?
If the sent_at
未设置时字段不存在:
db.emails.count({sent_at: {$exists: false}})
如果它存在且为空,或者根本不存在:
db.emails.count({sent_at: null})
如果它存在并且为空:
db.emails.count({sent_at: { $type: 10 }})
The 查询空或缺失字段MongoDB 手册的部分描述了如何查询空值和缺失值。
等式过滤器
The { item : null }
查询匹配包含值为以下的项目字段的文档null
or不包含item
field.
db.inventory.find( { item: null } )
存在性检查
以下示例查询不包含字段的文档。
The { item : { $exists: false } }
查询匹配不包含以下内容的文档item
field:
db.inventory.find( { item : { $exists: false } } )
类型检查
The { item : { $type: 10 } }
查询匹配only包含以下内容的文件item
其值为null
;即 item 字段的值为BSON类型 Null
(类型编号10
) :
db.inventory.find( { item : { $type: 10 } } )
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)