mongo的query中包含null怎么查询

本文介绍了在MongoDB中如何使用不同的查询操作符来查找包含null值或未定义字段的文档。通过具体示例展示了查询操作符如$exists、$type的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考http://docs.mongodb.org/manual/faq/developers/#how-do-i-query-for-fields-that-have-null-values

How do I query for fields that have null values?

Fields in a document may store null values, as in a notional collection, test, with the following documents:

{ _id: 1, cancelDate: null }
{ _id: 2 }

Different query operators treat null values differently:

  • The { cancelDate : null } query matches documents that either contains the cancelDatefield whose value is null or that do not contain the cancelDate field:

    db.test.find( { cancelDate: null } )
    

    The query returns both documents:

    { "_id" : 1, "cancelDate" : null }
    { "_id" : 2 }
    
  • The { cancelDate : { $type: 10 } } query matches documents that contains thecancelDate field whose value is null only; i.e. the value of the cancelDate field is of BSON Type Null (i.e. 10) :

    db.test.find( { cancelDate : { $type: 10 } } )
    

    The query returns only the document that contains the null value:

    { "_id" : 1, "cancelDate" : null }
    
  • The { cancelDate : { $exists: false } } query matches documents that do not contain the cancelDate field:

    db.test.find( { cancelDate : { $exists: false } } )
    

    The query returns only the document that does not contain the cancelDate field:

    { "_id" : 2 }
    

SEE ALSO

The reference documentation for the $type and $exists operators.




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值