Mongo使用

在用mongo进行查询时,$exists表示是否document是否包含这个field,即使field的value为null也算是包含。
$exists
Syntax: { field: { $exists: <boolean> } }

When <boolean> is true, $exists matches the documents that contain the field, including 
documents where the field value is null. If <boolean> is false, the query returns only 
the documents that do not contain the field.
Examples
Exists and Not Equal ToConsider the following example:db.inventory.find( { qty: 
{ $exists: true, $nin: [ 5, 15 ] } } )This query will select all documents in 
the inventory collection where the qty field exists and its value does 
not equal 5 or 15.

Null Values
Given a collection named records with the following documents:

{ a: 5, b: 5, c: null }
{ a: 3, b: null, c: 8 }
{ a: null, b: 3, c: 9 }
{ a: 1, b: 2, c: 3 }
{ a: 2, c: 5 }
{ a: 3, b: 2 }
{ a: 4 }
{ b: 2, c: 4 }
{ b: 2 }
{ c: 6 }
Consider the output of the following queries:

Query:

 db.records.find( { a: { $exists: true } } )
Result:

 { a: 5, b: 5, c: null }
{ a: 3, b: null, c: 8 }
{ a: null, b: 3, c: 9 }
{ a: 1, b: 2, c: 3 }
{ a: 2, c: 5 }
{ a: 3, b: 2 }
{ a: 4 }
Query:

 db.records.find( { b: { $exists: false } } )
Result:

 { a: 2, c: 5 }
{ a: 4 }
{ c: 6 }
Query:

 db.records.find( { c: { $exists: false } } )
Result:

 { a: 3, b: 2 }
{ a: 4 }
{ b: 2 }


 


而要查看document中哪些field的值为null,则需要用$in 来查询。详情可以参考http://docs.mongodb.org/manual/reference/operator/query/in/ 页面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值