MongoDB $mod操作符
$mod
操作符是什么
MongoDB提供了各种算术表达式操作符,$mod
操作符就是其中之一。$mod
操作符用于聚合管道阶段中,用于将一个数除以另一个数并返回余数。换句话说,第一个数是被除数,第二个数是除数。
$mod操作符的语法
{ $mod: [ < expression 1 >, < expression 2 > ] }
可以是解析为数字的任何有效表达式。
示例
在以下示例中,我们正在处理:
Database: javaTpoint
Collection: items
Document: Ten documents that contain the details of the items
>db.items.find().pretty()
{
{
"_id" : 1,
"item_name" : "Apple",
"total_Price" : null,
"quantity" : 40,
}
{
"_id" : 2,
"item_name" : "Banana",
"total_Price" : 1000,
"quantity" : 72,
}
{
"_id" : 3,
"item_name" : "Cherry",
"total_Price" : 215,
"quantity" : 25,
}
{
"_id" : 4,
"item_name" : "Apple",
"total_Price" : null,
"quantity" : 25,
}
{
"_id" : 5,
"item_name" : "Banana",
"total_Price" : 400,
"quantity" : 35,
}
{
"_id" : 6,
"item_name" : "Banana",
"total_Price" : 510,
"quantity" : 100,
}
{
"_id" : 7,
"item_name" : "Cherry",
"total_Price" : 500,
"quantity" : 41,
}
{
"_id" : 8,
"item_name" : "Rasbhari",
"total_Price" : 80,
"quantity" : "Ten",
}
{
"_id" : 9,
"item_name" : "Banana",
"total_Price" : 205,
"quantity" : 10,
}
{
"_id" : 10,
"item_name" : "Apple",
"total_Price" : 95,
"quantity" : null,
}
}
示例1:使用$mod运算符找到余数
在这个示例中,我们将使用$mod运算符将”total_Price”字段的值除以”quantity”字段的值,并找到余数。
db.item.aggregate(
[
{match: {item_name : "Banana"}},
{project:
{
item_name : 1,
total_Price : 1,
quantity : 1,
remainderValue : {mod: ["total_Price", "$quantity"]}
}
}
]
)
输出:
{
"_id" : 2,
"item_name" : "Banana",
"total_Price" : 1000,
"quantity" : 72,
"remainderValue" : 64
}
{
"_id" : 5,
"item_name" : "Banana",
"total_Price" : 400,
"quantity" : 35,
"remainderValue" : 15
}
{
"_id" : 6,
"item_name" : "Banana",
"total_Price" : 510,
"quantity" : 100,
"remainderValue" : 10
}
{
"_id" : 9,
"item_name" : "Banana",
"total_Price" : 205,
"quantity" : 10,
"remainderValue" : 5
}
示例2:数据类型错误
$mod
运算符仅支持数值数据类型。如果值是字符串,$mod
运算符会返回错误。
{ "_id" : 8, "item_name" : "Rasbhari", "total_Price" : 80, "quantity" : "Ten", }
db.item.aggregate(
[
{match: {item_name : "Rasbhari"}},
{project:
{
item_name : 1,
total_Price : 1,
quantity : 1,
remainderValue : {mod: ["total_Price", "$quantity"]}
}
}
]
)
输出:
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "$mod 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
示例3:空值
在这个示例中,我们将使用$mod
运算符在apple中通过将”total_Price”字段的值除以”quantity”字段来找到余数。
db.item.aggregate(
[
{match: {item_name : "Apple"}},
{project:
{
item_name : 1,
total_Price : 1,
quantity : 1,
remainderValue : {mod: ["total_Price", "$quantity"]}
}
}
]
)
输出:
{
"_id" : 1,
"item_name" : "Apple",
"total_Price" : null,
"quantity" : 40,
"remainderValue" : null
}
{
"_id" : 4,
"item_name" : "Apple",
"total_Price" : null,
"quantity" : 25,
"remainderValue" : null
}
{
"_id" : 10,
"item_name" : "Apple",
"total_Price" : 95,
"quantity" : null,
"remainderValue" : null
}