MongoDB $divide 运算符

2024 年 9 月 6 日 | 3 分钟阅读

MongoDB 中的 $divide 运算符是什么?

MongoDB 提供了各种算术表达式运算符。 $divide 运算符是其中之一。此运算符用于聚合管道阶段。它还用于将一个数字除以另一个数字并返回结果。

$divide 运算符的语法

要使用此运算符,您必须将参数传递到数组中。在此运算符中,第一个参数是被除数,第二个参数是除数。

示例

在下面的示例中,我们将使用

输出

>db.products.find().pretty()
{
    {
        "_id" : 1,
        "name" : "Mobile",
        "totalPrice" : 100000,
        "totalQuantity" : 50,
        "billYear" : 2018
    }
    {
        "_id" : 2,
        "name" : "Keyboard",
        "totalPrice" : 5000,
        "totalQuantity" : 10,
        "billYear" : 2017
    }
    {
        "_id" : 3,
        "name" : "Mouse",
        "totalPrice" : 2000,
        "totalQuantity" : 5,
        "billYear" : 2018
    }
    {
        "_id" : 4,
        "name" : "Memory Card",
        "totalPrice" : 2500,
        "totalQuantity" : 25,
        "billYear" : 2019
    }
    {
        "_id" : 5,
        "name" : "Mobile",
        "totalPrice" : 20000,
        "totalQuantity" : 4,
        "billYear" : 2020
    }
    {
        "_id" : 6,
        "name" : "Mobile",
        "totalPrice" : 25000,
        "totalQuantity" : 2,
        "billYear" : 2021
    }
    {
        "_id" : 7,
        "name" : "Memory Card",
        "totalPrice" : 1000,
        "totalQuantity" : 10,
        "billYear" : 2019
    }
    {
        "_id" : 8,
        "name" : "Pen drive",
        "totalPrice" : 15000,
        "totalQuantity" : "Two",
        "billYear" : 2018
    }
    {
        "_id" : 9,
        "name" : "Laptop",
        "billDetail" : {
                               "totalPrice" : 45000,
                               "totalQuantity" : 1,
                              }
        "billYear" : 2021
    }
    {
        "_id" : 10,
        "name" : "Memory Carde",
        "totalPrice" : 4000,
        "totalQuantity" : 50,
        "billYear" : 2020
    }
}

示例 1:使用 $divide 运算符

在此示例中,我们将使用 $divide 运算符来查找手机的价格。

输出

{
        "_id" : 1,
        "name" : "Mobile",
        "totalPrice" : 100000,
        "totalQuantity" : 50,
        "mobilePrice" : 2000
}
{
        "_id" : 5,
        "name" : "Mobile",
        "totalPrice" : 20000,
        "totalQuantity" : 4,
        "mobilePrice" : 5000
}
{
        "_id" : 6,
        "name" : "Mobile",
        "totalPrice" : 25000,
        "totalQuantity" : 2,
        "mobilePrice" : 12500
}

示例 2:添加您自己的数字

如果用户想用某个数字除一个字段,用户可以这样做。在此示例中,我们将“totalPrice”字段除以 2。

输出

{
        "_id" : 4,
        "name" : "Memory Card",
        "totalPrice" : 2500,
        "halfPrice" : 1250
}
{
        "_id" : 7,
        "name" : "Memory Card",
        "totalPrice" : 1000,
        "halfPrice" : 500
}
{
        "_id" : 10,
        "name" : "Memory Carde",
        "totalPrice" : 4000,
        "halfPrice" : 2000
}

示例 3:错误的数据类型

$divide 运算符仅支持整数数据类型。如果该值不是整数,则会产生错误。

输出

uncaught exception: Error: command failed: {
	"ok" : 0,
	"errmsg" : "$divide only supports numeric types, not string and double",
	"code" : 16611,
	"codeName" : "Location16611"
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:639:17
assert.commandWorked@src/mongo/shell/assert.js:729:16
DB.prototype._runAggregate@src/mongo/shell/db.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1058:12
@(shell):1:1

示例 4:在嵌入式文档中使用 $divide 运算符

在此示例中,我们将笔记本电脑的总价除以 2。

输出

{
        "_id" : 9,
        "name" : "Laptop",
        "halfPrice" : 22500
}