MongoDB乾貨篇之查詢

MongoDB乾貨篇之查詢

準備工做

在開始以前咱們應該先準備數據方便演示,這裏我插入的了幾條數據,數據以下:javascript

db.user.insertMany(
    [{
    name:'jack',
    age:22,
    sex:'Man',
    tags:['python','c++','c'],
    grades:[22,33,44,55],
    school:{
    name:'shida',
    city:'xuzhou'
    }
    },{
    name:'jhon',
    age:33,
    sex:null,
    tags:['python','java'],
    grades:[66,22,44,88],
    school:{
    name:'kuangda',
    city:'xuzhou'
    }
    },
    {
    name:'xiaoming',
    age:33,
    tags:['python','java'],
    grades:[66,22,44,88],
    school:{
    name:'kuangda',
    city:'xuzhou'
    }
    }
    ]
    )

find(<query filter>, <projection>)

其中query表示查找的條件,至關於mysqlwhere子句,projection列出你想要查找的數據,格式爲db.collection.find(find(<query filter>, <projection>))java

實例:

  • 下面不帶參數的查找,將會查找出全部的結果python

db.find().pretty();
    
    //輸出結果
    
    
{                                                     
        "_id" : ObjectId("59056f81299fe049404b2899"), 
        "name" : "jack",                              
        "age" : 22,                                   
        "tags" : [                                    
                "python",                             
                "c++",                                
                "c"                                   
        ],                                            
        "grades" : [                                  
                22,                                   
                33,                                   
                44,                                   
                55                                    
        ],                                            
        "school" : {                                  
                "name" : "shida",                     
                "city" : "xuzhou"                     
        }                                             
}
  • 下面找出知足namejack的數據,而且只輸出name,age,這裏的_id是默認輸出的,若是不想輸出將將它設置爲0,想要輸出那個字段將它設置爲1mysql

db.user.find({name:'jack'},{name:1,age:1})
    
    //輸出結果
    { "_id" : ObjectId("59056f81299fe049404b2899"), "name" : "jack", "age" : 22 }
    
    
    db.user.find({name:'jack'},{name:1,age:1,_id:0})
    
    //輸出結果
    {"name" : "jack", "age" : 22 }

**注意這裏的一個 projection不能 同時 指定包括和排除字段,除了排除 _id 字段。 在 顯式包括 字段的映射中,_id 字段是惟一一個您能夠 顯式排除 的。c++

查詢內嵌文檔

上述例子中插入的school數據就表示內嵌文檔git

徹底匹配查詢

徹底匹配查詢表示school中的查詢數組必須和插入的數組徹底同樣,順序都必須同樣才能查找出來github

db.user.find({name:'jack',school:{name:'shida',city:'xuzhou'}});
 
 //輸出結果
 
 { "_id" : ObjectId("59056f81299fe049404b2899"), "name" : "jack", "age" : 22, "tags" : [ "python", "c++", "c" ], "grades" : [ 22, 33, 44, 55 ], "school" : { "name" : "shida", "city" : "xuzhou" } }
 
 
 //下面是指定輸出的字段,這裏的school.name表示只輸出school文檔中name字段,必須加引號
 db.user.find({name:'jack',school:{name:'shida',city:'xuzhou'}},{name:1,age:1,'school.name':1});
 //輸出結果
 { "_id" : ObjectId("59056f81299fe049404b2899"), "name" : "jack", "age" : 22, "school" : { "name" : "shida" } }

鍵值對查詢

能夠經過鍵值對查詢,不用考慮順序,好比'school.name':'shida',表示查詢學校名字爲shida的數據,這裏的引號是必需要的sql

db.user.find({'school.name':'shida'},{name:1,school:1});
    
    //輸出結果
    
    { "_id" : ObjectId("59056f81299fe049404b2899"), "name" : "jack", "school" : { "name" : "shida", "city" : "xuzhou" } }

查詢操做符

下面咱們將配合查詢操做符來執行復雜的查詢操做,好比元素查詢、 邏輯查詢 、比較查詢操做。咱們使用下面的比較操做符"$gt""$gte""$lt""$lte"(分別對應">"">=""<""<=")express

實例

下面查詢年齡在20-30之間的信息json

db.user.find({
age:{$gt:20,$lt:30}  
})

//輸出
{ "_id" : ObjectId("59056f81299fe049404b2899"), "name" : "jack", "age" : 22, "tags" : [ "python", "c++", "c" ], "grades" : [ 22, 33, 44, 55 ], "school" : { "name" : "shida", "city" : "xuzhou" } }

$ne

$ne表示不相等,例如查詢年齡不等於22歲的信息

db.user.find({age:{$ne:22}})

//輸出
{ "_id" : ObjectId("59057c16f551d8c9003d31e0"), "name" : "jhon", "age" : 33, "tags" : [ "python", "java" ], "grades" : [ 66, 22, 44, 88 ], "school" : { "name" : "kuangda", "city" : "xuzhou" } }

slice

$slice操做符控制查詢返回的數組中元素的個數。此操做符根據參數{ field: value } 指定鍵名和鍵值選擇出文檔集合,而且該文檔集合中指定array鍵將返回從指定數量的元素。若是count的值大於數組中元素的數量,該查詢返回數組中的全部元素的。

語法:db.collection.find( { field: value }, { array: {$slice: count }});

  • 下面將查詢grades中的前兩個數

db.user.find({name:'jack'},{grades:{$slice:2},name:1,age:1,'school.name':1});

//輸出,能夠看出這裏的grades只輸出了前面兩個

{ "_id" : ObjectId("59057c16f551d8c9003d31df"), "name" : "jack", "age" : 22, "grades" : [ 22, 33 ], "school" : { "name" : "shida" } }
  • 下面將輸出後3個數據

db.user.find({name:'jhon'},{grades:{$slice:-3},name:1});

//輸出
{ "_id" : ObjectId("59057c16f551d8c9003d31e0"), "name" : "jhon", "grades" : [ 22, 44, 88 ] }
  • 下面介紹指定一個數組做爲參數。數組參數使用[ skip , limit ] 格式,其中第一個值表示在數組中跳過的項目數,第二個值表示返回的項目數。

db.user.find({name:'jack'},{grades:{$slice:[2,2]},name:1});  //這裏將會跳過前面的兩個,直接獲得後面的兩個數據


//輸出

{ "_id" : ObjectId("59057c16f551d8c9003d31df"), "name" : "jack", "grades" : [ 44, 55 ] }

$exists

若是$exists的值爲true,選擇存在該字段的文檔,若值爲false則選擇不包含該字段的文檔

下面將會查詢不存在sex這一項的信息

db.user.find({sex:{$exists:false}})

//結果
{ "_id" : ObjectId("59058460fe58ed1089f2a5cd"), "name" : "xiaoming", "age" : 33, "tags" : [ "python", "java" ], "grades" : [ 66, 22, 44, 88 ], "school" : { "name" : "kuangda", "city" : "xuzhou" } }


db.user.find({sex:{$exists:true}});

//結果
{ "_id" : ObjectId("59058460fe58ed1089f2a5cb"), "name" : "jack", "age" : 22, "sex" : "Man", "tags" : [ "python", "c++", "c" ], "grades" : [ 22, 33, 44, 55 ], "school" : { "name" : "shida", "city" : "xuzhou" } }
{ "_id" : ObjectId("59058460fe58ed1089f2a5cc"), "name" : "jhon", "age" : 33, "sex" : null, "tags" : [ "python", "java" ], "grades" : [ 66, 22, 44, 88 ], "school" : { "name" : "kuangda", "city" : "xuzhou" } }

$or

執行邏輯OR運算,指定一個至少包含兩個表達式的數組,選擇出至少知足數組中一條表達式的文檔。
語法:{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }

  • 下面將要查找age等於22或者age等於33的值

db.user.find({$or:[{age:22},{age:33}]})

//結果

{ "_id" : ObjectId("59058460fe58ed1089f2a5cb"), "name" : "jack", "age" : 22, "sex" : "Man", "tags" : [ "python", "c++", "c" ], "grades" : [ 22, 33, 44, 55 ], "school" : { "name" : "shida", "city" : "xuzhou" } }
{ "_id" : ObjectId("59058460fe58ed1089f2a5cc"), "name" : "jhon", "age" : 33, "sex" : null, "tags" : [ "python", "java" ], "grades" : [ 66, 22, 44, 88 ], "school" : { "name" : "kuangda", "city" : "xuzhou" } }
{ "_id" : ObjectId("59058460fe58ed1089f2a5cd"), "name" : "xiaoming", "age" : 33, "tags" : [ "python", "java" ], "grades" : [ 66, 22, 44, 88 ], "school" : { "name" : "kuangda", "city" : "xuzhou" } }
  • 下面將會查找出年齡爲22或者33而且姓名爲jack的人的信息

db.user.find({name:'jack',$or:[{age:33},{age:22}]})

//結果

{ "_id" : ObjectId("59058460fe58ed1089f2a5cb"), "name" : "jack", "age" : 22, "sex" : "Man", "tags" : [ "python", "c++", "c" ], "grades" : [ 22, 33, 44, 55 ], "school" : { "name" : "shida", "city" : "xuzhou" } }

$and

指定一個至少包含兩個表達式的數組,選擇出知足該數組中全部表達式的文檔。$and操做符使用短路操做,若第一個表達式的值爲「false」,餘下的表達式將不會執行。
語法:{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }

  • 下面將會查找年齡在20-30之間的信息,對於下面使用逗號分隔符的表達式列表,MongoDB會提供一個隱式的$and操做:

db.user.find({$and:[{age:{$gt:20}},{age:{$lt:30}}]})
//上述語句至關於db.user.find({age:{$gt:20},age:{$lt:30}})

//結果
{ "_id" : ObjectId("59058460fe58ed1089f2a5cb"), "name" : "jack", "age" : 22, "sex" : "Man", "tags" : [ "python", "c++", "c" ], "grades" : [ 22, 33, 44, 55 ], "school" : { "name" : "shida", "city" : "xuzhou" } }

$in

匹配鍵值等於指定數組中任意值的文檔。相似sqlin,只要匹配一個value就會輸出
語法:{ field: { $in: [<value1>, <value2>, ... <valueN> ] } }

  • 下面將會查找grades中存在22,33之間的任意一個數的信息

db.user.find({grades:{$in:[22,33]}})
 
 //輸出
 
{ "_id" : ObjectId("59058460fe58ed1089f2a5cb"), "name" : "jack", "age" : 22, "sex" : "Man", "tags" : [ "python", "c++", "c" ], "grades" : [ 22, 33, 44, 55 ], "school" : { "name" : "shida", "city" : "xuzhou" } }
{ "_id" : ObjectId("59058460fe58ed1089f2a5cc"), "name" : "jhon", "age" : 33, "sex" : null, "tags" : [ "python", "java" ], "grades" : [ 66, 22, 44, 88 ], "school" : { "name" : "kuangda", "city" : "xuzhou" } }
{ "_id" : ObjectId("59058460fe58ed1089f2a5cd"), "name" : "xiaoming", "age" : 33, "tags" : [ "python", "java" ], "grades" : [ 66, 22, 44, 88 ], "school" : { "name" : "kuangda", "city" : "xuzhou" } }

$nin

 匹配鍵不存在或者鍵值不等於指定數組的任意值的文檔。相似sqlnot in(SQL中字段不存在使用會有語法錯誤).

  • 查詢出grades中不存在100或者44的文檔

db.user.find({grades:{$nin:[100,44]}})

$not

執行邏輯NOT運算,選擇出不能匹配表達式的文檔 ,包括沒有指定鍵的文檔。$not操做符不能獨立使用,必須跟其餘操做一塊兒使用

語法:{ field: { $not: { <operator-expression> } } }

  • 查詢年齡不大於30的信息

db.user.find({age:{$not:{$gt:30}}})

//輸出
{ "_id" : ObjectId("59058460fe58ed1089f2a5cb"), "name" : "jack", "age" : 22, "sex" : "Man", "tags" : [ "python", "c++", "c" ], "grades" : [ 22, 33, 44, 55 ], "school" : { "name" : "shida", "city" : "xuzhou" } }

迭代遊標的查詢

學太高級語言的朋友都知道迭代的問題,像java,下面使用迭代的方法查詢

var cursor=db.usr.find();
    
    //這裏使用迭代輸出全部的數據
    while(cursor.hasNext())    //這裏的hasNext()是判斷是否下一個中還有可迭代的值,若是沒有返回false
    {
        printjson(cursor.next());     //這裏的cursor.next是迭代的輸出,printjson是代替print(tojson()) 
    }
    
    
    print cursor.count()    //輸出其中有多少個數據
    
    cursor.forEach(printjson);   //forEach輸出
    
    
    var document=cursor.toArray();     //將迭代對象轉換成數組
    
    print document[0];       //以數組的形式輸出

更多文章請看本人博客

相關文章
相關標籤/搜索