mongodb判断null值
来源:互联网 发布:仿真电路软件手机 编辑:程序博客网 时间:2024/05/23 01:15
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 cancelDate field whose value is null orthat 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 the cancelDate field whose value is nullonly; 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.
$type¶
- $type¶
Syntax: { field: { $type: <BSON type> } }
$type selects the documents where thevalue of the field is the specified BSON type.
Consider the following example:
db.inventory.find( { price: { $type : 1 } } )
This query will select all documents in the inventory collection where theprice field value is a Double.
If the field holds an array, the $type operator performs the type check against the array elements and not the field.
Consider the following example where the tags field holds an array:
db.inventory.find( { tags: { $type : 4 } } )
This query will select all documents in the inventory collection where thetags array contains an element that is itself an array.
If instead you want to determine whether the tags field is an array type, use the$where operator:
db.inventory.find( { $where : "Array.isArray(this.tags)" } )
See the SERVER-1475 for more information about the array type.
Refer to the following table for the available BSON types and their corresponding numbers.
TypeNumberDouble1String2Object3Array4Binary data5Undefined (deprecated)6Object id7Boolean8Date9Null10Regular Expression11JavaScript13Symbol14JavaScript (with scope)1532-bit integer16Timestamp1764-bit integer18Min key255Max key127MinKey and MaxKey compare less than and greater than all other possibleBSON element values, respectively, and exist primarily for internal use.
Note
To query if a field value is a MinKey, you must use the$type with -1 as in the following example:
db.collection.find( { field: { $type: -1 } } )
Example
Consider the following example operation sequence that demonstrates both type comparisonand the special MinKey and MaxKey values:
db.test.insert( {x : 3});db.test.insert( {x : 2.9} );db.test.insert( {x : new Date()} );db.test.insert( {x : true } );db.test.insert( {x : MaxKey } )db.test.insert( {x : MinKey } )db.test.find().sort({x:1}){ "_id" : ObjectId("4b04094b7c65b846e2090112"), "x" : { $minKey : 1 } }{ "_id" : ObjectId("4b03155dce8de6586fb002c7"), "x" : 2.9 }{ "_id" : ObjectId("4b03154cce8de6586fb002c6"), "x" : 3 }{ "_id" : ObjectId("4b031566ce8de6586fb002c9"), "x" : true }{ "_id" : ObjectId("4b031563ce8de6586fb002c8"), "x" : "Tue Jul 25 2012 18:42:03 GMT-0500 (EST)" }{ "_id" : ObjectId("4b0409487c65b846e2090111"), "x" : { $maxKey : 1 } }
To query for the minimum value of a shard key of asharded cluster, use the following operation when connected to themongos:
use configdb.chunks.find( { "min.shardKey": { $type: -1 } } )
- mongodb判断null值
- string 判断 null 值
- null值判断
- 判断sqldr空值NULL
- hive 空值、NULL判断
- hive 空值、null判断
- freemarker对于 null 值判断
- 判断(null) <null>
- freemarker中空值“”,null值的判断
- vb中判断SQL中的NULL值
- 操作变量要先判断Null值
- null值判断的一个避免错误
- 水晶报表关于 NULL 值判断
- MySQL判断字段值为null
- c# 判断 值 是否为NUll
- c# 判断 值 是否为NUll
- shell对NULL值的判断
- null值安全的相等判断
- 短信一次发送字节140个,如果超过140字节就会分为两条。这时如果第140个字节是中文的前半,那么第一条短信应该发送139字节。设计一个程序,读取原始信息,可以根据长度自动拆分信息转换为多条短信
- Cocos2dx笔记与收藏
- 项目分解的能力是成功做事的必备能力
- nginx之配置文件分析与开源软件配置文件结构设计
- Ado.net连接数据库查询表信息的多种方法
- mongodb判断null值
- Delphi XE5 Android Dialogs 对话框
- struts2的简单的编程步骤
- linux shell if 参数
- 最新VMware Workstation 9.0 / Player 5.0 / Fusion 5.0/VMware Tools 9.2.0 不同平台正式版下载
- 畅通工程/还是畅通工程HDU1232_HDU1233
- 关于Core Location-ios定位
- Android Custom state
- android 文档民间翻译版本