MongoDB $pow运算符
MongoDB中的$pow运算符是什么
MongoDB提供了多种算术表达式运算符。$pow运算符是其中之一。该运算符用于聚合管道阶段。该运算符用于计算一个数的指数(幂)。
什么是一个数的指数
指数(幂)是将一个数乘以自身的结果。通常,指数在书写中以如下图示表示。

$pow 运算符的语法
{ $pow: [ < number >, < exponent > ] }
< number > 和 < exponent >可以是任何有效的表达式,直到解析为一个数字为止。
重要的要点:
- 如果数字为null,则$pow运算符返回null。
- 如果数字引用的字段缺失,则$pow运算符返回null。
- 如果数字为NaN,则$pow运算符返回NaN。
| S.No | Example | Output | 
|---|---|---|
| 1. | { $pow: [ 4, 0 ] } | 1 | 
| 2. | { $pow: [ 4, 2 ] } | 16 | 
| 3. | { $pow: [ 4, -2 ] } | 0.0625 | 
| 4. | { $pow: [ -4, 0.5 ] } | NaN | 
示例
在下面的示例中,我们正在处理:
Database: javaTpoint
Collection: shapes
Document: Six documents that contain the details of the shapes
>db.shapes.find().pretty()
{
        {
         "_id" : 1, 
         "name" : "rectangle",
         "area" : 16
        }
        {
         "_id" : 2, 
         "name" : "square",
         "area" : 10
        }
        {
         "_id" : 3, 
         "name" : "circle",
         "perimeter" : 15,
         "area" : 10,
         "details" : { 
                           "radius" : 3,
                           "diameter" : 6
                          }
        }
        {
         "_id" : 4, 
         "name" : "rectangle",
         "area" : 0
        }
        {
         "_id" : 5, 
         "name" : "oval",
         "area" : 20
        }
        {
         "_id" : 6, 
         "name" : "triangle",
         "area" : 5
        }
        {
         "_id" : 7, 
         "name" : "rectangle",
         "area" : null
        }
}
示例1:使用 $pow 运算符
在这个示例中,我们使用 $pow 运算符将矩形形状中的“area”字段提升到指定的幂次。
db.shapes.aggregate(
 [
   {match: { name : "rectangle"}},
      {project: 
              {
                name : 1,
                area : 1,
                result : {pow: ["area", 3]}
               }
      }
 ]
)
输出:
{
         "_id" : 1, 
         "name" : "rectangle",
         "area" : 16,
         "result" : 4096
}
{
         "_id" : 4, 
         "name" : "rectangle",
         "area" : 0,
         "result" : 0
}
{
         "_id" : 7, 
         "name" : "rectangle",
         "area" : null,
         "result" : null
}
在这个示例中,我们将面积字段作为基数,将3作为指数。因此,矩形的每个”面积”字段都被提高了3次幂。
示例2:负指数 如果基数为零(0)且指数为负数,则无法提高这个数字。在这种情况下,会返回错误消息。
db.shapes.aggregate(
  [
    { match: { _id : 4 } },
      {project: 
           { 
                name : 1,
                area : 1,
                result: { pow: [ "area", -3 ] }
            }
       }
  ]
)
输出:
uncaught exception : Error : command failed : {
    "ok" : 0,
    "errmsg" : "$pow cannot take a base of 0.and a negative exponent",
    "code" : 28764,
    "codeName" : "Location28764"4' [ ln;' lj; h
Lh;klh[pjlkh[pkoh[khp[o
} : aggregate failed:
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:618:17
assert.commandWorked@src/mongo/shell/assert.js:708:16
DB.prototype._runAggregate@src/mongo/shell/db.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1046 : 12
@(shell) : 1 : 1
错误明确指出:” $pow 不能以0为底数和负指数”。
db.shapes.aggregate(
  [
    { match: { _id: 5  } },
      {project: 
           { 
                name : 1,
                area : 1,
                result: { pow: [ "area", -3 ] }
            }
       }
  ]
)
输出:
{
         "_id" : 5, 
         "name" : "oval",
         "area" : 20,
         "result" : 0.000125  
}
示例3:空指数
我们已经在”示例1″中看到,如果底数为空,结果也将为空。
如果指数的值为空,结果仍将为空。
db.shapes.aggregate(
  [
    { match: { _id: 6  } },
      {project: 
           { 
                name : 1,
                area : 1,
                result: { pow: [ "area", null ] }
            }
       }
  ]
)
输出:
{
         "_id" : 6, 
         "name" : "triangle",
         "area" : 5,
         "result" : null  
}
示例4:不存在的字段
如果在程序中应用$pow运算符到一个不存在的字段上,将返回null。
db.shapes.aggregate(
  [
    { match: { _id: 2  } },
      {project: 
           { 
                name : 1,
                area : 1,
                result: { pow: [ "perimeter", null ] }
            }
       }
  ]
)
输出:
{
         "_id" : 2, 
         "name" : "square",
         "area" : 10,
         "result" : null  
}
 极客笔记
极客笔记