mongodb查詢語法

mongodb查詢語法

所有操作分爲聚合查詢和非聚合查詢

聚合查詢中又分爲:聚合階段,和聚合操作

聚合階段主要對文檔進行:選擇,投影,分組,加減字段,排序,連接

聚合操作是用來實現聚合階段的功能。 如爲了實現選擇,可以使用數學表達式

 

匹配字段分兩類

關於查詢匹配分兩種:

這種方式,不能進行計算:{field:{$op, value}}

{$expr:{$op:[f1,f2..]}}支持複雜數值運算,使用邏輯and or可以支持多級匹配

 

操作分類

數據u,v兩個集合操作

1.匹配 滿足條件的行才被選出來 match

2.投影 選擇需要和不需要的字段 project

3.數組 選擇數組中的文檔的字段, 對數組進行範圍截取,數組攤平unwind, 求大小, 在數組中in

4.添加新字段 set add

5.兩表連接u.FieldA=v.FieldA lookup, graphLookup

6.限制輸出數量limit

7.排序 sort

8.分段 bucket,按某一個字段的值進行分段

9.分組 group, 按某一個字段進行分組

10.求合sum,平均average,計數count,最大max, 最小min,

11.過濾、filter, 字段A過濾後變成字段B

12.綁定值到變量 let

13.比較:> >= < <=

14.邏輯:and or not nor

15.數學運算: + - * / abs acos achsh cos sin tan 連接cat 子串substr 指數 對數 開方 取模

16.取整:ceil floor 四捨五入round

17.日期操作:字符串到日期,日期到字符串。取日期中的年月日小時

18.隨機,第一個first

19. 數字,字符串轉換 toDate, toDecimal toDouble toInt toLong toString toLower strlen** 分隔split trim

20. 合併集合merge

21.表達式求值expr,可以使用2元操作$op:[f,g]

操作符分爲1元,2元,3元,n元

1元操作符:{field:{$op, value}}, 如存在exists, 非not

2元操作符:{field1:{$op, field2}}, 如> <

{$op:[field1, field2]}, 如+, -, >, <

3元操作符:$cond

n元操作符:{$op:valueName} 如sum, count

 

示例代碼

db.stu.insertMany([
    {"name":"wyg","age":28, "score":98},
    {"name":"hy","age":28, "score":40},
    {"name":"wl","age":27, "score":90},
    {"name":"seq","age":28, "score":66},
    {"name":"zj","age":18, "score":80},
    {"name":"adson","age":28, "score":98},
    {"name":"jack","age":30, "score":83},
    {"name":"frac","age":32, "score":76}
])
//聚合操作,使用groupby
db.stu.aggregate([
  {
    "$group": {
      "_id":"$age",
      "scores":{"$addToSet":"$score"}
    }
  }
])
//計數
db.stu.aggregate([
  {
    "$count":"numOfPeople"
  }
])
//{ "numOfPeople" : 8 }
//添加新字段
db.stu.aggregate(
[
  {
    "$facet": {
      "name":[{"$project":{"name":1}}],
      "sumOfAge":[{$group:{_id:0,"sum":{"$sum":"$age"}}}],
    }
  }
]).pretty()
//添加新字段
db.stu.aggregate([
  {
    "$set":{
      "sumOfScore":{
        "$sum":"$score"
      }
    }
  }
])
//分組並添加新字段
db.stu.aggregate(
[
  {
    "$group": {
      _id:0,
      sumOfAge:{$sum:"$age"},
      sumOfScore:{$sum:"$score"}
    }
  },
  {
    "$project":{
      "_id":0,
      "sumOfAge":1,
      "sumOfScore":1
    }
  }
])
//抽影並添加新字段
db.stu.aggregate([{"$project":{"_id":0, "name":1, "total":{$add:['$age','$score']}}}])
//按區間分組
db.stu.aggregate([
    {
        $bucket:{
            groupBy:"$score",
            boundaries:[0, 60, 70, 80, 90, 100],
            default:"Other",
            output:{
                count:{$sum:1},
            }
        }
    }
])

db.artists.insertMany([
  { "_id" : 1, "last_name" : "Bernard", "first_name" : "Emil", "year_born" : 1868, "year_died" : 1941, "nationality" : "France" },
  { "_id" : 2, "last_name" : "Rippl-Ronai", "first_name" : "Joszef", "year_born" : 1861, "year_died" : 1927, "nationality" : "Hungary" },
  { "_id" : 3, "last_name" : "Ostroumova", "first_name" : "Anna", "year_born" : 1871, "year_died" : 1955, "nationality" : "Russia" },
  { "_id" : 4, "last_name" : "Van Gogh", "first_name" : "Vincent", "year_born" : 1853, "year_died" : 1890, "nationality" : "Holland" },
  { "_id" : 5, "last_name" : "Maurer", "first_name" : "Alfred", "year_born" : 1868, "year_died" : 1932, "nationality" : "USA" },
  { "_id" : 6, "last_name" : "Munch", "first_name" : "Edvard", "year_born" : 1863, "year_died" : 1944, "nationality" : "Norway" },
  { "_id" : 7, "last_name" : "Redon", "first_name" : "Odilon", "year_born" : 1840, "year_died" : 1916, "nationality" : "France" },
  { "_id" : 8, "last_name" : "Diriks", "first_name" : "Edvard", "year_born" : 1855, "year_died" : 1930, "nationality" : "Norway" }
])
//按區間分組,並添加新字段
db.artists.aggregate( [
  // First Stage
  {
    $bucket: {
      groupBy: "$year_born",                        // Field to group by
      boundaries: [ 1840, 1850, 1860, 1870, 1880 ], // Boundaries for the buckets
      default: "Other",                             // Bucket id for documents which do not fall into a bucket
      output: {                                     // Output for each bucket
        "count": { $sum: 1 },
        "artists" :
          {
            $push: {
              "name": { $concat: [ "$first_name", " ", "$last_name"] },
              "year_born": "$year_born"
            }
          }
      }
    }
  },
  // Second Stage
  {
    $match: { count: {$gt: 3} }
  }
] )

//計數
db.orders.aggregate({$count:"num"})
//分組求合
db.orders.aggregate([{$group:{_id:"$status","num":{$sum:1}}}])

db.orders.findOne()


db.sales.insertMany([
  { "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") },
  { "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") },
  { "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") },
  { "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" :  NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") },
  { "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") },
  { "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") },
  { "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") },
  { "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") },
])
//按多個字段分組
db.sales.aggregate([{$group:{
  "_id":{"day":{"$dayOfYear":"$date"},"year":{"$year":"$date"}},
  "itemSold":{"$addToSet":"$item"}
}}])
//計數
db.sales.aggregate([
    {
        $group:{
            _id:null,
            count:{$sum:1}
        }
    }
])

//分組計算並過濾
db.sales.aggregate([
    {
        $group:{
            _id:'$item',
            totalSaleAmount:{
                $sum:{
                    $multiply:["$price","$quantity"]
                }
            }
        }
    },
    {
        $match : {
            "totalSaleAmount":{
                $gte : 100
            }
        }
    }
])


db.orders.insert([
   { "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 },
   { "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 },
   { "_id" : 3  }
])

db.inventory.insert([
   { "_id" : 1, "sku" : "almonds", description: "product 1", "instock" : 120 },
   { "_id" : 2, "sku" : "bread", description: "product 2", "instock" : 80 },
   { "_id" : 3, "sku" : "cashews", description: "product 3", "instock" : 60 },
   { "_id" : 4, "sku" : "pecans", description: "product 4", "instock" : 70 },
   { "_id" : 5, "sku": null, description: "Incomplete" },
   { "_id" : 6 }
])

//左外連接兩個表
db.orders.aggregate([
   {
     $lookup:
       {
         from: "inventory",
         localField: "item",
         foreignField: "sku",
         as: "inventory_docs"
       }
  }
])

db.classes.insert( [
   { _id: 1, title: "Reading is ...", enrollmentlist: [ "giraffe2", "pandabear", "artie" ], days: ["M", "W", "F"] },
   { _id: 2, title: "But Writing ...", enrollmentlist: [ "giraffe1", "artie" ], days: ["T", "F"] }
])
db.members.insert( [
   { _id: 1, name: "artie", joined: new Date("2016-05-01"), status: "A" },
   { _id: 2, name: "giraffe", joined: new Date("2017-05-01"), status: "D" },
   { _id: 3, name: "giraffe1", joined: new Date("2017-10-01"), status: "A" },
   { _id: 4, name: "panda", joined: new Date("2018-10-11"), status: "A" },
   { _id: 5, name: "pandabear", joined: new Date("2018-12-01"), status: "A" },
   { _id: 6, name: "giraffe2", joined: new Date("2018-12-01"), status: "D" }
])
//左外連接兩個表
db.classes.aggregate([
   {
      $lookup:
         {
            from: "members",
            localField: "enrollmentlist",
            foreignField: "name",
            as: "enrollee_info"
        }
   }
])

db.inventory.insertOne({ "item" : "ABC1", sizes: [ "S", "M", "L"] })
db.inventory.aggregate( [ { $unwind : "$sizes" },{$project:{_id:0}} ] )
db.userA.insertMany([
  {"username":"wyg", "addr":"shenzheng", "tel":"653245", "course":"CS"},
  {"username":"hy", "addr":"wuhan", "tel":"18365822", "course":"PE"},
  {"username":"frac", "addr":"beijing", "tel":"11223", "course":"MATH"}
])
//字段存在
db.stu.find({"tel":{$exists:1}})

db.stu.aggregate([{
  "$lookup": {
    "from":"userA",
    "localField":"name",
    "foreignField":"username",
    "as":"user"
  }
},
  {"$project":{
    "_id":0
    }
  },
  {
    "$unwind": "$user"
  },
  {"$project":{
    "user._id":0
    }
  }
])

db.report.insert([
  { "_id" : 1, "name" : "Dev" },
{ "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" },
{ "_id" : 3, "name" : "Ron", "reportsTo" : "Eliot" },
{ "_id" : 4, "name" : "Andrew", "reportsTo" : "Eliot" },
{ "_id" : 5, "name" : "Asya", "reportsTo" : "Ron" },
{ "_id" : 6, "name" : "Dan", "reportsTo" : "Andrew" }
])
//圖查詢
db.report.aggregate([{
  "$graphLookup" : {
    "from":"report",
    "startWith":"$reportsTo",
    "connectFromField":"reportsTo",
    "connectToField":"name",
    "as":"reportHierarchy",
    "maxDepth":10
  }
}])

db.mp3.insertMany([ 
  {"val":1, "name":"A", "tar":"C"},
  {"val":2, "name":"B", "tar":"D"},
  {"val":3, "name":"C", "tar":"D"},
  {"val":4, "name":"D", "tar":"B"}
  ])
db.mp.insertMany([ 
  {"val":1, "name":"A", "tar":["B","C"]},
  {"val":2, "name":"B", "tar":["D","E"]},
  {"val":3, "name":"C", "tar":["E","F"]},
  {"val":4, "name":"D", "tar":["F","A"]}
  ])
db.mp.insertOne({"val":1, "name":"A", "tar":["F","H"]})
db.src.insertMany([
  {"uname":"A", "age":28, "addr":"shenzheng"},
  {"uname":"B", "age":30, "addr":"hangzhou"}
])
//過濾,圖查詢,投影
db.src.aggregate([
  {
    "$match":{
      "age":{
        "$gte":20
      },
      "uname":{
        "$exists":1
      }
    }
  },{
    "$graphLookup":{
      "from":"mp",
      "startWith":"$uname",
      "connectFromField":"tar",
      "connectToField":"name",
      "as":"next",
      "maxDepth":10
    }
  },
  {
    "$project":{
      "_id":0,
      "uname":1,
      "next.name":1,
      "next.tar":1
    }
  }
])

//選擇,然後連接
db.src.aggregate([
  {
    "$match":{
      "age":{
        "$gte":20
      },
      "uname":{
        "$exists":1
      }
    }
  },
  {
    "$lookup":{
      "from":"mp",
      "localField":"uname",
      "foreignField":"name",
      "as":"next",
    }
  }
  ])


  db.book.insertMany([
    {"name":"A", "price":10, "count":10},
    {"name":"B", "price":9, "count":12},
    {"name":"C", "price":6, "count":7},
    {"name":"D", "price":7, "count":9},
    {"name":"E", "price":8, "count":10},
    {"name":"F", "price":5, "count":20}
  ])
  db.book.aggregate([
    {
      "$match":{
        "$expr":{
        "$gte":[
          {"$multiply":["$price","$count"]},
          100
          ]
        }
      }
    }
  ])

db.u.insertMany([
  {"FieldA":"a1", "FieldB":"b1", "FieldC":[{"w":0,"h":6},{"w":10,"h":19}]},
  {"FieldA":"a2", "FieldB":"b2", "FieldC":[{"w":1,"h":5},{"w":11,"h":18}]},
  {"FieldA":"a3", "FieldB":"b3", "FieldC":[{"w":2,"h":4},{"w":12,"h":17}]},
  {"FieldA":"a4", "FieldB":"b4", "FieldC":[{"w":3,"h":3},{"w":13,"h":16}]},
  {"FieldA":"a5", "FieldB":"b5", "FieldC":[{"w":4,"h":2},{"w":14,"h":15}]}
  ])
db.v.insertMany([
  {"FieldA":"a1", "FieldD":1, "FieldE":20, "arr":[10,18,30]},
  {"FieldA":"a2", "FieldD":2, "FieldE":21, "arr":[12,20,40]},
  {"FieldA":"a3", "FieldD":3, "FieldE":22, "arr":[16,59,35]},
  {"FieldA":"a4", "FieldD":4, "FieldE":23, "arr":[15,22,36]},
  {"FieldA":"a5", "FieldD":6, "FieldE":24, "arr":[14,20,30]}
])
//二元操作 等於=查詢
db.v.find({"FieldD":{"$eq":2}})
//使用用expr進行計算
db.v.find({"$expr":{
  "$and":[
    {"$gte":[{"$size":"$arr"},3]},
    {"$lte":[{"$multiply":["$FieldD","$FieldE"]},60]},
    {"$gte":[{"$sum":"$arr"},60]} //對數組求和
  ]
}})

//if then else 示例
db.v.find({"$expr":{
  "$gte":[{"$cond":{
    "if":{"$gte":["$FieldD",3]},
    "then":{"$multiply":["$FieldE", 3]},
    "else":{"$multiply":["$FieldE", 4]}
  }},70]
}}).sort({"FieldD":-1})

 

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