mongodb exp 操作符號總結(jié)

exp

  • $ne : 不等于某個值
qtyGt250: { $ne: [ "$qty", 250 ] }
  • $eq : 等于某個值
qtyGt250: { $eq: [ "$qty", 250 ] }
  • $gt : 大于某個值
qtyGt250: { $gt: [ "$qty", 250 ] }
  • $in : 包含某些值,也可以用正則匹配
 "has bananas" : {
        $in: [ "bananas", "$in_stock" ]
      }
  • $ln : log^e
 y: { $ln: "$sales"  }
  • $lt : 小于
 qtyLt250: { $lt: [ "$qty", 250 ] }
  • $or : 符合其中一個
 result: { $or: [ { $gt: [ "$qty", 250 ] }, { $lt: [ "$qty", 200 ] } ] }
  • $add : 添加
 billing_date: { $add: [ "$date", 3*24*60*6000]}
  • $and : &&
 result: { $and: [ { $gt: [ "$qty", 250 ] }, { $lt: [ "$qty", 200 ] } ] }
  • $cmp : Compares two values and returns
  • -1 if the first value is less than the second.
  • 1 if the first value is greater than the second.
  • 0 if the two values are equivalent.
 cmpTo250: { $cmp: [ "$qty", 250 ] }
  • $map : 批量修改
 { adjustedGrades:
            {
              $map:
                 {
                   input: "$quizzes",
                   as: "grade",
                   in: { $add: [ "$$grade", 2 ] }
                 }
            }
         }
  • $not : 取相反
 result: { $not: [ { $gt: [ "$qty", 250 ] } ] }
  • $zip : 行轉(zhuǎn)列
 {
      $zip: {
        inputs: [
          { $arrayElemAt: [ "$matrix", 0 ] },
          { $arrayElemAt: [ "$matrix", 1 ] },
          { $arrayElemAt: [ "$matrix", 2 ] },
        ]
      }
    }

{ matrix: [[1, 2], [2, 3], [3, 4]] },
{ matrix: [[8, 7], [7, 6], [5, 4]] },

{ "transposed" : [ [ 1, 2, 3 ], [ 2, 3, 4 ] ] }
{ "transposed" : [ [ 8, 7, 5 ], [ 7, 6, 4 ] ] }

  • $conf : 判斷

{ "_id" : 1, "item" : "abc1", qty: 300 }
{ "_id" : 2, "item" : "abc2", qty: 200 }
{ "_id" : 3, "item" : "xyz1", qty: 250 }

{
    $cond: { if: { $gte: [ "$qty", 250 ] }, then: 30, else: 20 }
}

{ "_id" : 1, "item" : "abc1", "discount" : 30 }
{ "_id" : 2, "item" : "abc2", "discount" : 20 }
{ "_id" : 3, "item" : "xyz1", "discount" : 30 }

  • 日期組
 {
           year: { $year: "$date" },
           month: { $month: "$date" },
           day: { $dayOfMonth: "$date" },
           hour: { $hour: "$date" },
           minutes: { $minute: "$date" },
           seconds: { $second: "$date" },
           milliseconds: { $millisecond: "$date" },
           dayOfYear: { $dayOfYear: "$date" },
           dayOfWeek: { $dayOfWeek: "$date" }
}

  • $size:數(shù)量
 numberOfColors: { $size: "$colors" }
  • $type:類型
 {
       a : { $type: "$a" }
    }
  • $range:劃分數(shù)組
 "Rest stops": { $range: [ 0, "$distance", 25 ] }

{ _id: 0, city: "San Jose", distance: 42 }
{ _id: 1, city: "Sacramento", distance: 88 }
{ _id: 2, city: "Reno", distance: 218 }
{ _id: 3, city: "Los Angeles", distance: 383 }

{ "city" : "San Jose", "Rest stops" : [ 0, 25 ] }
{ "city" : "Sacramento", "Rest stops" : [ 0, 25, 50, 75 ] }
{ "city" : "Reno", "Rest stops" : [ 0, 25, 50, 75, 100, 125, 150, 175, 200 ] }
{ "city" : "Los Angeles", "Rest stops" : [ 0, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375 ] }
  • $slice:取前三
 threeFavorites: { $slice: [ "$favorites", 3 ]}
  • $ifNull:若為空則設(shè)置默認值.
 description: { $ifNull: [ "$description", "Unspecified" ] }
  • $switch : 分支判斷
 {
          $switch:
            {
              branches: [
                {
                  case: { $gte : [ { $avg : "$scores" }, 90 ] },
                  then: "Doing great!"
                },
                {
                  case: { $and : [ { $gte : [ { $avg : "$scores" }, 80 ] },
                                   { $lt : [ { $avg : "$scores" }, 90 ] } ] },
                  then: "Doing pretty well."
                },
                {
                  case: { $lt : [ { $avg : "$scores" }, 80 ] },
                  then: "Needs improvement."
                }
              ],
              default: "No scores found."
            }
         }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,936評論 0 33
  • 這都是我自己的理解 人都是命中注定 我以前總是看到電視上一些人說我的命運我要自己做主 那時的我感覺...
    在看吧閱讀 160評論 0 0
  • 最近搞了臺Mac Pro 3.1,自己吭哧吭哧的換硬件,搞系統(tǒng),也算是能用了。 完了呢,在蘋果底下裝系統(tǒng)使用的U盤...
    Ryankong閱讀 15,313評論 0 6

友情鏈接更多精彩內(nèi)容