MongoDB $divide 运算符
MongoDB中的$divide
运算符是什么
MongoDB提供了各种算术表达式运算符。$divide
运算符是其中之一。这个运算符在聚合管道阶段中使用。它也用于将一个数除以另一个数并返回结果。
$divide运算符的语法
{ $divide : [ < expression 1 >, < expression 2 > ] }
使用此运算符,您需要将参数传递给一个数组。在此运算符中,第一个参数是被除数,第二个参数是除数。
示例
在以下示例中,我们使用如下:
Database: javaTpoint
Collection: products
Document: Ten documents that contain the details of each product
输出:
>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运算符来查找手机的价格。
db.products.aggregate(
[
{match: { name : "Mobile"}},
{project:
{
name : 1,
totalPrice : 1,
totalQuantity : 1,
mobilePrice : {divide: ["totalPrice", "$totalQuantity"]}
}
}
]
)
输出:
{
"_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。
db.products.aggregate(
[
{match: { name : "Memory Card"}},
{project:
{
name : 1,
totalPrice : 1,
halfPrice : { divide : [ "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运算符仅支持整数数据类型。如果值不是整数,则会出现错误。
db.products.aggregate(
[
{match: {name : "Pen drive"}},
{project:
{
item_name : 1,
total_Price : 1,
Price : {divide : ["totalPrice", "$totalQuantity"]}
}
}
]
)
输出:
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。
db.products.aggregate(
[
{match: { name : "Laptop"}},
{project:
{
name : 1,
halfPrice : {divide: ["billDetail.totalPrice", 2]}
}
}
]
)
输出:
{
"_id" : 9,
"name" : "Laptop",
"halfPrice" : 22500
}