美文网首页
mongodb exp 操作符号总结

mongodb exp 操作符号总结

作者: 许道龙 | 来源:发表于2017-03-30 11:15 被阅读0次

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 : 行转列
 {
      $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:数量
 numberOfColors: { $size: "$colors" }
  • $type:类型
 {
       a : { $type: "$a" }
    }
  • $range:划分数组
 "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:若为空则设置默认值.
 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."
            }
         }

相关文章

网友评论

      本文标题:mongodb exp 操作符号总结

      本文链接:https://www.haomeiwen.com/subject/joyuottx.html