当前位置:数据库 > mongodb >>

MongoDB入门6——查询(二)

3.查询中null的处理
 
        null的匹配非常有意思:null不仅仅会匹配到指定键的值确实等于null的文档,并且还会匹配到查询所制定键不存在的文档。例如,插入下面这三个文档:
 
 
> db.users.insert({"name":"Tom","age":20,"job":"Sales"});  
> db.users.insert({"name":"Sam","age":25,"job":"Manager"});  
> db.users.insert({"name":"Jim","age":25});  
> db.users.insert({"name":"Sam","age":25,"job":null}); 
 
> db.users.insert({"name":"Tom","age":20,"job":"Sales"});
> db.users.insert({"name":"Sam","age":25,"job":"Manager"});
> db.users.insert({"name":"Jim","age":25});
> db.users.insert({"name":"Sam","age":25,"job":null});        现在我们用传统的方式来匹配job为null的文档:
 
 
> db.users.find({"job":null}); 
 
> db.users.find({"job":null});        结果如下:
 
 
{ "_id" : ObjectId("4f0714e3edb28db4864be582"), "name" : "Jim", "age" : 25 }  
{ "_id" : ObjectId("4f071542edb28db4864be583"), "name" : "Sam", "age" : 25, "job" : null } 
 
{ "_id" : ObjectId("4f0714e3edb28db4864be582"), "name" : "Jim", "age" : 25 }
{ "_id" : ObjectId("4f071542edb28db4864be583"), "name" : "Sam", "age" : 25, "job" : null }        很明显,不仅将job确实为null的文档匹配出来,而且将没有job这个键的文档匹配出来了。很多时候这并不是我们的本意。有一个$exists操作符可以来解决这个问题。先看代码:
 
 
db.users.find({"job":{"$in":[null],"$exists":true}}); 
 
db.users.find({"job":{"$in":[null],"$exists":true}});        结果如下:
 
 
{ "_id" : ObjectId("4f071542edb28db4864be583"), "name" : "Sam", "age" : 25, "job" : null } 
 
{ "_id" : ObjectId("4f071542edb28db4864be583"), "name" : "Sam", "age" : 25, "job" : null }4.正则表达式
 
        MongoDB使用Perl兼容的正则表达式库来匹配正则表达式。正则表达式本身非常强大,有专门写这个的书,我这里不针对这个,只是简单的举个例子。例如,忽略大小写情况下匹配name为Tom的文档:
 
 
db.users.find({"name":/tom/i}); 
 
db.users.find({"name":/tom/i});5.数组查询
 
        数组查询大的思路:大部分情况下,数组的每个元素都可以是对应键的值。这么说肯定不好理解,举个例子,插入下面这样一个文档:
 
 
db.food.insert({"fruit":["apple","banana","peach"]}); 
 
db.food.insert({"fruit":["apple","banana","peach"]});        现在执行下面三个查询,都会将这个文档匹配出来。
 
 
db.food.find({"fruit":"apple"});  
db.food.find({"fruit":"banana"});  
db.food.find({"fruit":"peach"}); 
 
db.food.find({"fruit":"apple"});
db.food.find({"fruit":"banana"});
db.food.find({"fruit":"peach"});        如果需要匹配fruit键的值既有apple又有banana的文档,可以使用$all匹配符。现在我们假设有下面几个文档:
 
 
{ "_id" : ObjectId("4f071a10edb28db4864be584"), "fruit" : [ "apple", "banana", "peach" ] }  
{ "_id" : ObjectId("4f071c5676285076f80ca7c7"), "fruit" : [ "apple", "banana" ] }  
{ "_id" : ObjectId("4f071c6076285076f80ca7c8"), "fruit" : [ "apple" ] }  
{ "_id" : ObjectId("4f071ce576285076f80ca7c9"), "fruit" : [ "banana", "apple" ] }  
 
{ "_id" : ObjectId("4f071a10edb28db4864be584"), "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : ObjectId("4f071c5676285076f80ca7c7"), "fruit" : [ "apple", "banana" ] }
{ "_id" : ObjectId("4f071c6076285076f80ca7c8"), "fruit" : [ "apple" ] }
{ "_id" : ObjectId("4f071ce576285076f80ca7c9"), "fruit" : [ "banana", "apple" ] }          执行下面的查询:
 
 
db.food.find({"fruit":{"$all":["apple","banana"]}}); 
 
db.food.find({"fruit":{"$all":["apple","banana"]}});        可以匹配到下面的三个文档:
 
 
{ "_id" : ObjectId("4f071a10edb28db4864be584"), "fruit" : [ "apple", "banana", "peach" ] }  
{ "_id" : ObjectId("4f071c5676285076f80ca7c7"), "fruit" : [ "apple", "banana" ] }  
{ "_id" : ObjectId("4f071ce576285076f80ca7c9"), "fruit" : [ "banana", "apple" ] } 
 
{ "_id" : ObjectId("4f071a10edb28db4864be584"), "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : ObjectId("4f071c5676285076f80ca7c7"), "fruit" : [ "apple", "banana" ] }
{ "_id" : ObjectId("4f071ce576285076f80ca7c9"), "fruit" : [ "banana", "apple" ] }        你可能会说,可以采用精确匹配整个数组的方法如下:
 
 
db.food.find({"fruit":["apple","banana"]}); 
 
db.food.find({"fruit":["apple","banana"]});        得到的结果只有一个文档:
 
 
{ "_id" : ObjectId("4f071c5676285076f80ca7c7"), "fruit" : [ "apple", "banana" ] } 
 
{ "_id" : ObjectId("4f071c5676285076f80ca7c7"), "fruit" : [ "apple", "banana" ] }        不用惊讶,这就是精确匹配,连apple和banana出现的顺序都不能调换。这种结果很显然不是很多时候我们想要的。也是为什么引进$all匹配操作符。
 
        还有一种匹配的语法:可以使用key.index来匹配指定键对应值(是数组的情况下)的第index+1个元素值。还是上面水果的那个例子,我们假设要匹配fruit键对应值第三个元素为peach的文档,我们可以这么写:
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,