查询关键词:
- $or
多个条件 满足其中一个 即可:
Syntax:{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }
db.inventory.find( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } )
以下两查询是等价的:
db.inventory.find( { $or: [ { quantity: { $qe: 20 } }, { quantity:{$qe:50}} ] } ) = db.inventory.find ( { quantity: { $in: [20, 50] } } )
- $and
$and 关键词,我们在使用时,常常是省略了。
Syntax:{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }
db. inventory.find( { price: { $ne: 1.99, $exists: true } } )
功能待价于: 价格不等于1.990,且:价格字段是存在的
db.inventory.find( { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } )
再看一个比较复杂的例子:
db.inventory.find( { $and : [ { $or : [ { price : 0.99 }, { price : 1.99 } ] }, { $or : [ { sale : true }, { qty : { $lt : 20 } } ] } ] } )
- $not,$nor
Syntax: { field: { $not: { <operator-expression> } } }
示例1:
db.inventory.find( { price: { $not: { $gt: 1.99 } } } )
示例2 (正则:‘不以p.开头’)
db.inventory.find( { item: { $not: /^p.*/ } } )
$nor 可以理解为也不是,也不是。也可以理解为多个 $or的值都不成立
Syntax: { $nor: [ { <expression1> }, { <expression2> }, ... { <expressionN> } ] }
示例1:
>db.inventory.find({$nor:[{price:1.99},{qty:{$lt:20}},{sale:true}]},{_id:0,name:1,price:1,qty:1,sale:1}) { "name" : "orange", "price" : 3.99, "qty" : 300 } { "name" : "apple", "price" : 1, "qty" : 50 } > db.inventory.find({},{_id:0,name:1,price:1,qty:1,sale:1}) { "name" : "banana", "price" : 1.99, "qty" : 100, "sale" : true } { "name" : "orange", "price" : 3.99, "qty" : 300 } { "name" : "apple", "price" : 1, "qty" : 50 } { "name" : "peach", "price" : 2.1, "qty" : 10 } >
- $in,$nin
直接上示例代码:
db.inventory.find( { qty: { $in: [ 5, 15 ] } } )
正则查询中,也可以使用
db.inventory.find( { tags: { $in: [ /^be/, /^st/ ] } } )
的 $nin 比较不常用,但比使用$not更方便
db.inventory.find( { qty: { $nin: [ 5, 15 ] } } )
- $exists
Syntax: { field: { $exists: <boolean> } }
> db.inventory.find({sale:{$exists:1}},{_id:0,name:1,price:1,qty:1,sale:1}) { "name" : "banana", "price" : 1.99, "qty" : 100, "sale" : true } > db.inventory.find({sale:{$exists:true}},{_id:0,name:1,price:1,qty:1,sale:1}) { "name" : "banana", "price" : 1.99, "qty" : 100, "sale" : true }
从上面示例中,可以看到,判断一个字段是否存在,使用 $exists:1 $exists:true都是可以的。
- null
> db.tnull.find() { "_id" : 1, "a" : 2, "b" : 3, "c" : null } { "_id" : 2, "a" : 3, "b" : null, "c" : 8 } { "_id" : 3, "a" : null, "b" : 4, "c" : 9 } { "_id" : 4, "b" : 5, "c" : 10 } > db.tnull.find({a:{$exists:1}}) { "_id" : 1, "a" : 2, "b" : 3, "c" : null } { "_id" : 2, "a" : 3, "b" : null, "c" : 8 } { "_id" : 3, "a" : null, "b" : 4, "c" : 9 } > db.tnull.find({a:null}) { "_id" : 3, "a" : null, "b" : 4, "c" : 9 } { "_id" : 4, "b" : 5, "c" : 10 } > db.tnull.find({a:{$exists:0}}) { "_id" : 4, "b" : 5, "c" : 10 } >
一个值是否为null ,只要注意 如果一记录中,字段不存在,也会返回的。
如果你要查询值为null,并且 不返回 字段存在的,那你必须加上 $exists:1
- $where
> db.foo.find() { "_id" : ObjectId("53c747ffcc5792ae0fef1ca0"), "apple" : 1, "banana" : 6, "peach" : 3 } { "_id" : ObjectId("53c7480ccc5792ae0fef1ca1"), "apple" : 8, "banana" : 4, "peach" : 4 }
关于这个查询,我们先来看看上面数据。如果我 需要返回有两个字段相同的文档,也就是要返回如下文档
那这时我要怎样查询呢,使用前面的查询是无法实现的。{ "_id" : ObjectId("53c7480ccc5792ae0fef1ca1"), "apple" : 8, "banana" : 4, "peach" : 4 }
那么这样$where 出场了。
> db.foo.find({"$where":function(){ ... for(var current in this){ ... for(var other in this){ ... if(current != other && this[current] == this[other]){ ... return true; ... } ... } ... } ... return false; ... }}) { "_id" : ObjectId("53c7480ccc5792ae0fef1ca1"), "apple" : 8, "banana" : 4, "peach" : 4 } >
但是,要注意以下说明:
不是非常必要时,一定要避免使 用”$where”查询,因为效率太低,相当的。文档在MongoDB中是以BSON格式保存的,在$where查询时,每个文档都要从BSON转换为javascript对象然后再通过”$where”中的表达式来运行。有时可以将常规查询作为前置过滤,再使用”$where”查询对结果进行调优
我对上面代码做了点修改,以方便理解。上面代码说得简单一点就是两个循环,每一条记录各字段进行循环,看是否有两个字段值是相等的,如果相等返回.
db.foo.find({"$where":function(){ for (var banana in this) { for (var peach in this) { if (banana != peach && this[banana]==this[peach]) { return true;} } } return false;} }) { "_id" : ObjectId("53c7480ccc5792ae0fef1ca1"), "apple" : 8, "banana" : 4, "peach" : 4 } >
- $type
在mongodb 中,我们知道同一字段可以存储不同的数据类型数据,我们看下面的数据示例:
那就面临一个问题,怎样进行不同条件的查询????
> 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() { "_id" : ObjectId("53c762f4cc5792ae0fef1ca2"), "x" : 3 } { "_id" : ObjectId("53c762f4cc5792ae0fef1ca3"), "x" : 2.9 } { "_id" : ObjectId("53c762f4cc5792ae0fef1ca4"), "x" : ISODate("2014-07-17T05:45:24.032Z") } { "_id" : ObjectId("53c762f4cc5792ae0fef1ca5"), "x" : true } { "_id" : ObjectId("53c762f4cc5792ae0fef1ca6"), "x" : { "$maxKey" : 1 } } { "_id" : ObjectId("53c762f4cc5792ae0fef1ca7"), "x" : { "$minKey" : 1 } }
如果我要查询x 值是数值的数据,要怎要查询呢????> db.test.find({x:{$type:1}}) { "_id" : ObjectId("53c762f4cc5792ae0fef1ca2"), "x" : 3 } { "_id" : ObjectId("53c762f4cc5792ae0fef1ca3"), "x" : 2.9 } >
我们可以再结合此关键词,做一个查询:
> db.test.insert({x:5}) > db.test.find({$and:[{x:{$gte:3}},{x:{$type:1}}]}) { "_id" : ObjectId("53c762f4cc5792ae0fef1ca2"), "x" : 3 } { "_id" : ObjectId("53c76e8bcc5792ae0fef1ca8"), "x" : 5 } >
可以看到,还是很方便的。
最后列出$type 对应的 值 列表:
Refer to the following table for the available BSON types and their corresponding numbers.
Type Number-----------------------------------------------
Double 1
String 2
Object 3
Array 4
Binary data 5
Undefined (deprecated) 6
Object id 7
Boolean 8
Date 9
Null 10
Regular Expression 11
JavaScript 13
Symbol 14
JavaScript (with scope) 15
32-bit integer 16
Timestamp 17
64-bit integer 18
Min key 255
Max key 127