【知识小课堂】mongodb 之 查询关键词使用
查詢關(guān)鍵詞:
- $or
多個(gè)條件 滿足其中一個(gè) 即可:
Syntax:{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }
db.inventory.find( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } )以下兩查詢是等價(jià)的:
- $and
$and 關(guān)鍵詞,我們?cè)谑褂脮r(shí),常常是省略了。
Syntax:{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] } db. inventory.find( { price: { $ne: 1.99, $exists: true } } )
功能待價(jià)于: 價(jià)格不等于1.990,且:價(jià)格字段是存在的
db.inventory.find( { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } )再看一個(gè)比較復(fù)雜的例子:
db.inventory.find( { $and : [{ $or : [ { price : 0.99 }, { price : 1.99 } ] }, { $or : [ { sale : true }, { qty : { $lt : 20 } } ] } ] } )
- $not,$nor
示例1:
db.inventory.find( { price: { $not: { $gt: 1.99 } } } )示例2? (正則:‘不以p.開頭’)
db.inventory.find( { item: { $not: /^p.*/ } } )$nor 可以理解為也不是,也不是。也可以理解為多個(gè) $or的值都不成立
示例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 ] } } )正則查詢中,也可以使用
的 $nin?比較不常用,但比使用$not更方便
db.inventory.find( { qty: { $nin: [ 5, 15 ] } } )
- $exists
> 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 }
從上面示例中,可以看到,判斷一個(gè)字段是否存在,使用 $exists:1? $exists:true都是可以的。
- null
一個(gè)值是否為null ,只要注意 如果一記錄中,字段不存在,也會(huì)返回的。
如果你要查詢值為null,并且 不返回 字段存在的,那你必須加上 $exists:1
- $where
關(guān)于這個(gè)查詢,我們先來看看上面數(shù)據(jù)。
如果我 需要返回有兩個(gè)字段相同的文檔,也就是要返回如下文檔
{ "_id" : ObjectId("53c7480ccc5792ae0fef1ca1"), "apple" : 8, "banana" : 4, "peach" : 4 }那這時(shí)我要怎樣查詢呢,使用前面的查詢是無(wú)法實(shí)現(xiàn)的。那么這樣$where 出場(chǎng)了。
> 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 } >但是,要注意以下說明:
不是非常必要時(shí),一定要避免使 用”$where”查詢,因?yàn)樾侍?#xff0c;相當(dāng)?shù)摹N臋n在MongoDB中是以BSON格式保存的,在$where查詢時(shí),每個(gè)文檔都要從BSON轉(zhuǎn)換為javascript對(duì)象然后再通過”$where”中的表達(dá)式來運(yùn)行。有時(shí)可以將常規(guī)查詢作為前置過濾,再使用”$where”查詢對(duì)結(jié)果進(jìn)行調(diào)優(yōu)
我對(duì)上面代碼做了點(diǎn)修改,以方便理解。上面代碼說得簡(jiǎn)單一點(diǎn)就是兩個(gè)循環(huán),每一條記錄各字段進(jìn)行循環(huán),看是否有兩個(gè)字段值是相等的,如果相等返回.
- $type
在mongodb 中,我們知道同一字段可以存儲(chǔ)不同的數(shù)據(jù)類型數(shù)據(jù),我們看下面的數(shù)據(jù)示例:
那就面臨一個(gè)問題,怎樣進(jìn)行不同條件的查詢????
> 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 值是數(shù)值的數(shù)據(jù),要怎要查詢呢???? > db.test.find({x:{$type:1}}) { "_id" : ObjectId("53c762f4cc5792ae0fef1ca2"), "x" : 3 } { "_id" : ObjectId("53c762f4cc5792ae0fef1ca3"), "x" : 2.9 } >
我們可以再結(jié)合此關(guān)鍵詞,做一個(gè)查詢:
可以看到,還是很方便的。
最后列出$type 對(duì)應(yīng)的 值 列表:
?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
總結(jié)
以上是生活随笔為你收集整理的【知识小课堂】mongodb 之 查询关键词使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【知识小课堂】 mongodb 之字段中
- 下一篇: MONGODB 数据库文件读取的优化