mongodb條件查詢實現結構化查詢語句SQL

mongodb條件查詢


● 條件操作符

"$lt"===================>"<"
"$lte"==================>"<="
"$gt"===================>">"
"$gte"==================>">="
"$ne"===================>"!="
如:某集合B集合中文檔有屬性x值爲整數,需查找1<x<=10的文檔,寫法如下:
db.B.find({"x":{"$gt":1,"$lte":10}})
如:從某集合B中查找日期屬性day值大於2015/01/01的文檔數據,寫法如下:
db.B.find({"day":{"$gt":new Date("2015/01/01")}})


$in包含/$nin不包含

$in:查詢匹配指定條件值的文檔;
$nin:查詢不匹配指定條件值的文檔;
SQL:寫法:字段 in ('值1','值2'.....)
mongodb:db.B.find({"x":{"$in":['值1','值2',.....]}})
SQL:寫法:字段 not in ('值1','值2'.....)
mongodb:db.B.find({"x":{"$nin":['值1','值2',.....]}})
$in/$nin優點:可指定不同類型條件和值。


  $or或查詢

$or:查詢匹配多個條件多個值的文檔;
SQL:寫法:字段1 = 'xxx' or 字段2 in ( 'xxx').....
mongodb:db.B.find({"$or":[{"x":{"$in":['值1','值2'...]}},{"y":"3"}]})


$all匹配所有

比如文檔:
{"name":jack,"age":[1,2,3]}
{"name":jack,"age":[1,4,3]}
db.B.find({"age":{"$all":[2,3]}})結果:{"name":jack,"age":[1,2,3]}


$exists 判斷文檔屬性是否存在

db.B.find({"name":{"$exists":true}})   --查找屬性name存在的文檔
db.B.find({"name":{"$exists":false}})  --查找屬性name不存在的文檔


屬性值爲null情況

如下操作即可知道:
> db.C.find()
{ "_id" : ObjectId("5018fccd1781352fe25bf511"), "a" : "14", "b" : "14" }
{ "_id" : ObjectId("5018fccd1781352fe25bf512"), "a" : "15", "b" : "15" }
{ "_id" : ObjectId("5018fccd1781352fe25bf510"), "a" : "13", "b" : "13", "c" : null }
> db.C.find({"c":null})
{ "_id" : ObjectId("5018fccd1781352fe25bf511"), "a" : "14", "b" : "14" }
{ "_id" : ObjectId("5018fccd1781352fe25bf512"), "a" : "15", "b" : "15" }
{ "_id" : ObjectId("5018fccd1781352fe25bf510"), "a" : "13", "b" : "13", "c" : null }
可見查詢屬性c值爲null文檔,包括屬性c值爲null、該屬性c不存在兩個部分。若想只查詢屬性c爲null的文檔如下:
> db.C.find({"c":{"$in":[null],"$exists":true}})
{ "_id" : ObjectId("5018fccd1781352fe25bf510"), "a" : "13", "b" : "13", "c" : null }


$mod取模運算

db.B.find({"age":{"$mod":[5,1]}}) --表示查找年齡除5餘1的所有文檔
若查找年齡除5餘1之外的所有文檔,可結合$not運算:
db.B.find({"age":{"$not":{"$mod":[5,1]}}})


正則表達式

db.B.find({"name":/jack/i})


$size

> db.C.find()
{ "_id" : ObjectId("501e71557d4bd700257d8a41"), "a" : "1", "b" : [ 1, 2, 3 ] }
{ "_id" : ObjectId("501e71607d4bd700257d8a42"), "a" : "1", "b" : [ 1, 2 ] }
> db.C.find({"b":{"$size":2}})
{ "_id" : ObjectId("501e71607d4bd700257d8a42"), "a" : "1", "b" : [ 1, 2 ] }

$slice
返回數組的一個子集,即對以某屬性爲基礎,返回多少條(範圍)。也可以接受偏移值和要返回的元素的數量,來返回中間的結果。

> db.C.find()
{ "_id" : ObjectId("501e71557d4bd700257d8a41"), "a" : "1", "b" : [ 1, 2, 3 ] }
{ "_id" : ObjectId("501e71607d4bd700257d8a42"), "a" : "1", "b" : [ 1, 2 ] }
> db.C.findOne({},{"b":{"$slice":[2,3]}})
{ "_id" : ObjectId("501e71557d4bd700257d8a41"), "a" : "1", "b" : [ 3 ] }
> db.C.findOne({},{"b":{"$slice":-2}})
{
        "_id" : ObjectId("501e71557d4bd700257d8a41"),
        "a" : "1",
        "b" : [
                2,
                3
        ]
}

$where

即可執行任務javascript作爲查詢的一部分。
$where的值可以是function、也可以是字符串等等。
db.C.find({"$where":function(){return this.a == "1"}})與db.C.find({"$where":"this.a == '1'"}})
注意:採用$where子句查詢在速度上較常規查詢慢的多。因文檔需要從BSON轉換成javascript對象,然後通過"$where"的表達式來運行。

不用利用索引。可用常規查詢做前置過濾,配置"$where"查詢進行調優,可達到不犧牲性能的要求。

以上是mongodb使用時的一些條件查詢關鍵字,使用的時候不斷的去熟悉掌握。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章