1. 程式人生 > 實用技巧 >MongoDB學習筆記:文件Crud Shell

MongoDB學習筆記:文件Crud Shell

原文:https://blog.csdn.net/leshami/article/details/52901240

一、查詢語法

db.collection.find( <query filter>, <projection> )
db.collection.findOne() //僅僅返回單個文件,相當於使用limit

<query filter> 查詢的過濾條件
<projection> 投影,即哪些列需要返回
對於查詢的結果可以新增limits, skips, sort 等方式控制返回的結果集
預設情況下,在mongo shell中對於未使用將結果集返回給變數的情形下,僅返回前20條記錄

注:本文描述中有些地方使用到了文件的鍵值對,稱為鍵和值,有些地方稱為列,是一個概念。

二、準備資料

//演示環境
    db.version()  
    3.2.9

    //插入演示資料
    db.users.insertMany(
      [
         {
           _id: 1,
           name: "sue",
           age: 19,
           type: 1,
           status: "P",
           favorites: { artist: "Picasso", food: "pizza" },
           finished: [ 17, 3 ],
           badges: [ "blue", "black" ],
           points: [
              { points: 85, bonus: 20 },
              { points: 85, bonus: 10 }
           ]
         },
         {
           _id: 2,
           name: "bob",
           age: 42,
           type: 1,
           status: "A",
           favorites: { artist: "Miro", food: "meringue" },
           finished: [ 11, 25 ],
           badges: [ "green" ],
           points: [
              { points: 85, bonus: 20 },
              { points: 64, bonus: 12 }
           ]
         },
         {
           _id: 3,
           name: "ahn",
           age: 22,
           type: 2,
           status: "A",
           favorites: { artist: "Cassatt", food: "cake" },
           finished: [ 6 ],
           badges: [ "blue", "red" ],
           points: [
              { points: 81, bonus: 8 },
              { points: 55, bonus: 20 }
           ]
         },
         {
           _id: 4,
           name: "xi",
           age: 34,         //Author : Leshami
           type: 2,         //Blog   : http://blog.csdn.net/leshami
           status: "D",
           favorites: { artist: "Chagall", food: "chocolate" },
           finished: [ 5, 11 ],
           badges: [ "red", "black" ],
           points: [
              { points: 53, bonus: 15 },
              { points: 51, bonus: 15 }
           ]
         },
         {
           _id: 5,
           name: "xyz",
           age: 23,
           type: 2,
           status: "D",
           favorites: { artist: "Noguchi", food: "nougat" },
           finished: [ 14, 6 ],
           badges: [ "orange" ],
           points: [
              { points: 71, bonus: 20 }
           ]
         },
         {
           _id: 6,
           name: "abc",
           age: 43,
           type: 1,
           status: "A",
           favorites: { food: "pizza", artist: "Picasso" },
           finished: [ 18, 12 ],
           badges: [ "black", "blue" ],
           points: [
              { points: 78, bonus: 8 },
              { points: 57, bonus: 7 }
           ]
         }
      ]
    )

三、演示查詢

1.簡單查詢

 //查詢所有文件,文件太多,此處及以下演示查詢結果省略
    db.users.find( {} )     //與方式等價於db.users.find()
    db.users.findOne( {} )  //查詢單條記錄

    //等值查詢,{ <field1: <value1, ... }
    db.users.find({_id:5}).pretty()
    db.users.find({age:19,status:"P"})     //多條件等值查詢,隱式使用$and運算子

2.基於運算子的查詢

更多運算子:https://docs.mongodb.com/manual/reference/operator/query/

//基於運算子的查詢,{ <field1: { <operator1: <value1 }, ... }
    //基於$in運算子
    db.users.find( { status: { $in: [ "P", "D" ] } } )   

    //基於$and運算子的查詢
    db.users.find( {$and: [{ status: "A", age: { $lt: 30 } } ]})
    db.users.find( { status: "A", age: { $lt: 30 } } ) //此查詢方法與上一條等價,隱式使用$and運算子

    //基於$or運算子的查詢
    db.users.find( { $or: [ { status: "A" }, { age: { $lt: 30 } }]})

    //多條件組合查詢,基於$lt,也有$or,還有隱式$and
    db.users.find(
       {
         status: "A",
         $or: [ { age: { $lt: 30 } }, { type: 1 } ]
       }
    )

3、內嵌文件查詢

//等值匹配內嵌文件
    db.users.find( { favorites: { artist: "Picasso", food: "pizza" } } )

    //等值匹配內嵌文件的特定鍵值,通過"鍵.成員名:值"的方式來進行
    db.users.find( { "favorites.artist": "Picasso" } )

4、陣列查詢

 //查詢陣列元素            //查詢陣列badges中包含black的文件 
    db.users.find( { badges: "black" } ) 

    //匹配一個特定的陣列元素  //查詢陣列badges中第一個元素為black的文件
    db.users.find( { "badges.0": "black" } )  //此處0表示陣列的下標

    //匹配單個數組元素滿足條件  //查詢陣列finished至少有一個元素的值大於15且小於20的文件
    db.users.find( { finished: { $elemMatch: { $gt: 15, $lt: 20 } } } )

    //匹配混合陣列元素滿足條件  //查詢陣列finished中任意的一個元素大於15,且另外一個元素小於20
    db.users.find( { finished: { $gt: 15, $lt: 20 } } )  //或者這個元素既大於15又小於20的文件

    //查詢陣列內嵌文件          //查詢陣列points元素1內嵌文件鍵points的值小於等於55的文件
    db.users.find( { 'points.0.points': { $lte: 55 } } )

    //查詢陣列內嵌文件         //查詢陣列points內嵌文件鍵points的值小於等於55的文件,此處未指定陣列下標
    db.users.find( { 'points.points': { $lte: 55 } } )

    //查詢陣列元素至少一個內嵌文件滿足所有條件的文件
    //如下,陣列points內至少一個文件points鍵的值小於等於70,bonus鍵的值等於20的記錄,這樣的文件被返回
    db.users.find( { points: { $elemMatch: { points: { $lte: 70 }, bonus: 20 } } } )

    //查詢陣列元素任意一個內嵌文件滿足所有條件的文件
    //如下,陣列points內嵌文件任意一個文件points的值小於等於70,且陣列內另外一個文件bonus值等於20
    //或者陣列內某個內嵌文件points的值小於等於70,bonus的值等於20,這2種情形會被返回
    db.users.find( { "points.points": { $lte: 70 }, "points.bonus": 20 } )

四、限制查詢返回的結果集

 { field1: <value>, field2: <value> ... }
    1 or true  顯示該欄位
    0 or false 不顯示該欄位

1、限制返回的列

//查詢結果中顯示欄位name及status,預設情況下,文件的_id列會被返回
    > db.users.find( { status: "A" }, { name: 1, status: 1 } )
    { "_id" : 2, "name" : "bob", "status" : "A" }
    { "_id" : 3, "name" : "ahn", "status" : "A" }
    { "_id" : 6, "name" : "abc", "status" : "A" }

    //查詢結果中顯示欄位name及status,且不顯示_id列
    > db.users.find( { status: "A" }, { name: 1, status: 1, _id: 0 } )
    { "name" : "bob", "status" : "A" }
    { "name" : "ahn", "status" : "A" }
    { "name" : "abc", "status" : "A" }

    //返回查詢中未列出的全部列名
    > db.users.find( { status: "A" }, { favorites: 0, points: 0 ,badges:0})
    { "_id" : 2, "name" : "bob", "age" : 42, "type" : 1, "status" : "A", "finished" : [ 11, 25 ] }
    { "_id" : 3, "name" : "ahn", "age" : 22, "type" : 2, "status" : "A", "finished" : [ 6 ] }
    { "_id" : 6, "name" : "abc", "age" : 43, "type" : 1, "status" : "A", "finished" : [ 18, 12 ] }

    //返回內嵌文件指定的列名,相反地,如果不顯示內嵌文件的某個列,將在置0即可
    > db.users.find(
           { status: "A" },
           { name: 1, status: 1, "favorites.food": 1 }
        )
    { "_id" : 2, "name" : "bob", "status" : "A", "favorites" : { "food" : "meringue" } }
    { "_id" : 3, "name" : "ahn", "status" : "A", "favorites" : { "food" : "cake" } }
    { "_id" : 6, "name" : "abc", "status" : "A", "favorites" : { "food" : "pizza" } }

    //返回陣列內內嵌文件的指定列,如下查詢為陣列points內嵌文件bonus列
    > db.users.find( { status: "A" },{ name: 1,"points.bonus": 1 } )
    { "_id" : 2, "name" : "bob", "points" : [ { "bonus" : 20 }, { "bonus" : 12 } ] }
    { "_id" : 3, "name" : "ahn", "points" : [ { "bonus" : 8 }, { "bonus" : 20 } ] }
    { "_id" : 6, "name" : "abc", "points" : [ { "bonus" : 8 }, { "bonus" : 7 } ] }

    //下面的查詢使用了$slice操作符,這將僅僅返回符合status為A,且顯示陣列中的最後一個元素
    > db.users.find( { status: "A" }, { name: 1, status: 1, points: { $slice: -1 } } )
    { "_id" : 2, "name" : "bob", "status" : "A", "points" : [ { "points" : 64, "bonus" : 12 } ] }
    { "_id" : 3, "name" : "ahn", "status" : "A", "points" : [ { "points" : 55, "bonus" : 20 } ] }
    { "_id" : 6, "name" : "abc", "status" : "A", "points" : [ { "points" : 57, "bonus" : 7 } ] }

2、查詢NULL值或不存在的鍵

 //插入文件
    > db.users.insert(
       [
          { "_id" : 900, "name" : null },
          { "_id" : 901 },
          { "_id" : 902,"name" : "Leshami" ,"blog" : "http://blog.csdn.net/leshami"}  
       ]
    )

    //查詢name自動為null的文件,注,以下查詢中,不存在name列的文件_id:901的也被返回
    > db.users.find( { name: null } )
    { "_id" : 900, "name" : null }
    { "_id" : 901 }

    //通過$type方式返回name為null的文件,此時_id:901未返回
    > db.users.find( { name : { $type: 10 } } )
    { "_id" : 900, "name" : null }

    //通過$exists返回name自動不存在的文件
    > db.users.find( { name : { $exists: false } } )
    { "_id" : 901 }

五、查詢小結
1、文件查詢db.users.find()等價於db.users.find( {} )
2、基於and運算子的多個組合條件可以省略and運算子的多個組合條件可以省略and,直接將條件組合即可
3、對於$and運算子內的條件,用[]括起來,相當於陣列形式
4、對於陣列查詢,可以使用基於下標的方式精確配置特定的元素值
5、對於內嵌文件,可以使用”文件鍵.內嵌文件鍵”方式進行訪問
6、對於陣列內內嵌文件的方式,可以使用”陣列名.下標.內嵌文件鍵”方式訪問
7、對於哪些列名需要顯示可以通過{ field1: <0|1>, … }來設定
8、本文參考:https://docs.mongodb.com/manual/tutorial/query-documents/