比较运算符是我们学习任何语言或系统中最为常见的运算符之一。mongoDB的比较运算符,跟Linux的差不多,只不过每一个比较运算符前面会带有符号,他们分别是$eq、$gt、$gte、$lt、$lte、$ne、$in、nin等,下面将对这几个运算符进行描述。
一、比较运算符
$eq = "="
$gt (greater than ) >
$gte >= (equal)
$lt (less than) <
$lte <= (equal)
$ne (not equal) !=
$in in
$nin (not in) !in
重点:所有的比较运算符都是出现在键与值得中间,示例如下
{ <field_name>: { $operator: <value> } }
{ <ename>: { $eq: "robin" } }
{ <qty>: { $gt: 20 } }
二、比较运算符示例
1. $eq
{ <field>: {\$eq: <value> } }
$eq表达式与{ field: <value> }等价
<1> 支持简单匹配
<2> 支持文档匹配(document,逐字段比较)
<3> 支持数组匹配(array,有顺序要求)
//单值匹配
db.persons.find( { age: { $eq: 25 } } )
db.persons.find( { age: 25 } ) //与上一条语句等价
//多值匹配
db.persons.find( { age: { $eq: 25 } ,country: "USA"} )
db.persons.find({$and:[{age:{$eq:25}},{country:{$eq:"USA"}}]}) //也可以使用$and运算符
//嵌套文档匹配
db.persons.find( { score: {"c" : 75, "m" : 63,"e" : 97}} )
db.persons.find({"score.c":{$eq:89}}) //可以使用基于.成员的方式进行匹配
db.persons.find({"score.c":89}) //等价的查询方式
//数组匹配
db.persons.find({"books":{$eq:"PHP"}}) //forech查找,不包含A的数组不会被返回
db.persons.find({"books":{$in:["PHP"]}}) //也可以使用$in方式替代
db.persons.find({"books":"PHP"}) //等价方式
2. $gt/$gte
{field: {$gt: value} }
db.persons.find( { age: { $gt: 20 } } ) //大于运算符
db.persons.find( { age: { $gte: 25 } } ).sort({"age":-1}) //大于等于运算符及倒序排列
3. $lt/$lte
Syntax: {field: {$lt: value} }
db.persons.find( { age: { $lt: 25 } } )
db.persons.find( { age: { $lte: 25 } } ).sort({"age":-1})
db.persons.find( { age: { $lte: 25 } } )
4. $ne
Syntax: {field: {$ne: value} }
db.persons.find( { age: { $ne: 20 } } ).sort({"age":-1})
5. $in
Syntax: { field: { $in: [<value1>, <value2>, ... <valueN> ] } }
db.persons.find( { age: { $in: [ 25, 27 ] } } ) //多值匹配
db.persons.find( { books: { $in: [ /^JA/, /^PH/ ] } } ) //正则表达式匹配,查询以JA以及PH开头的
//Author : Leshami
//Blog : http://blog.csdn.net/leshami
6. $nin
Syntax: { field: { $nin: [ <value1>, <value2> ... <valueN> ]} }
db.persons.find( { age: { $nin: [ 25, 27 ] } } ).sort({"name":-1});
db.persons.update({ books: { $nin:["JAVA","PHP"]} },{$set: { age:100 }}) //基于$nin的文档更新
三、演示用到的示例文档
var persons = [{
name:"robinson.cheng",
age:25,
email:"robinson.cheng@qq.com",
score:{c:89,m:96,e:87},
country:"USA",
books:["JS","C++","EXTJS","MONGODB"]
},
{
name:"tom.tong",
age:25,
email:"tom.tong@qq.com",
score:{c:75,m:66,e:97},
country:"USA",
books:["PHP","JAVA","EXTJS","C++"]
},
{
name:"jerry.liu",
age:26,
email:"jerry.liu@qq.com",
score:{c:75,m:63,e:97},
country:"USA",
books:["JS","JAVA","C#","MONGODB"]
},
{
name:"henry.ye",
age:27,
email:"henry.ye@qq.com",
score:{c:89,m:86,e:67},
country:"China",
books:["JS","JAVA","EXTJS","MONGODB"]
},
{
name:"lisi",
age:26,
email:"lisi@qq.com",
score:{c:53,m:96,e:83},
country:"China",
books:["JS","C#","PHP","MONGODB"]
},
{
name:"fred.shu",
age:27,
email:"fred.shu@qq.com",
score:{c:45,m:65,e:99},
country:"China",
books:["JS","JAVA","C++","MONGODB"]
},
{
name:"jason.wu",
age:27,
email:"jason.wu@qq.com",
score:{c:99,m:96,e:97},
country:"China",
books:["JS","JAVA","EXTJS","PHP"]
},
{
name:"david.xiao",
age:26,
email:"david.xiao@dbsupport.cn",
score:{c:39,m:54,e:53},
country:"Korea",
books:["JS","C#","EXTJS","MONGODB"]
},
{
name:"frank.hu",
age:27,
email:"frank.hu@dbsupport.cn",
score:{c:35,m:56,e:47},
country:"Korea",
books:["JS","JAVA","EXTJS","MONGODB"]
},
{
name:"joe.zhou",
age:21,
email:"joe.zhou@dbsupport.cn",
score:{c:36,m:86,e:32},
country:"Korea",
books:["JS","JAVA","PHP","MONGODB"]
},
{
name:"steve.tang",
age:22,
email:"steve.tang@dbsupport.cn",
score:{c:45,m:63,e:77},
country:"Korea",
books:["JS","JAVA","C#","MONGODB"]
}]
for(var i = 0;i<persons.length;i++){
db.persons.insert(persons[i])
}
var persons = db.persons.find({name:"robinson.cheng"})
while(persons.hasNext()){
obj = persons.next();
print(obj.books.length)
}