MongoDBでは、$subtractを使用できます 数値や日付を減算するための集計パイプライン演算子。
具体的には、$subtract 次の3つのことができます:
- 2つの数値を減算して差を返します
- 日付から数値(ミリ秒単位)を減算し、結果の日付を返します
- 2つの日付を減算して、ミリ秒単位の差を返します
$subtract 演算子は値を引数として受け入れます。引数は、数値や日付に解決される限り、任意の有効な式にすることができます。日付から数値を引くには、日付を最初の引数にする必要があります。
サンプルデータ
dataというコレクションがあるとします。 次のドキュメントで:
{
"_id" : 1,
"a" : 20000,
"b" : 250,
"start" : ISODate("2021-01-03T00:00:00Z"),
"end" : ISODate("2021-01-03T23:30:15.100Z")
} 数値を引く
$subtractを使用できます aを減算する演算子 bのフィールド フィールド(またはその逆)。
例:
db.data.aggregate(
[
{ $project: {
_id: 0,
a: 1,
b: 1,
result: {
$subtract: [ "$a", "$b" ] } }
}
]
) 結果:
{ "a" : 20000, "b" : 250, "result" : 19750 } 日付から数値を引く
最初の引数が日付で、2番目の引数が数値の場合、$subtract 演算子は、ミリ秒単位の日付から数値を減算します。
例:
db.data.aggregate(
[
{ $project: {
_id: 0,
b: 1,
start: 1,
result: {
$subtract: [ "$start", "$b" ] } }
}
]
).pretty() 結果:
{
"b" : 250,
"start" : ISODate("2021-01-03T00:00:00Z"),
"result" : ISODate("2021-01-02T23:59:59.750Z")
} 日付から250ミリ秒が差し引かれていることがわかります。
日付から数値を引く場合、日付は最初の引数で、数値は2番目の引数である必要があります。
引数を入れ替えるとどうなりますか:
db.data.aggregate(
[
{ $project: {
_id: 0,
b: 1,
start: 1,
result: {
$subtract: [ "$b", "$start" ] } }
}
]
).pretty() 結果:
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "cant $subtract adate from a double",
"code" : 16556,
"codeName" : "Location16556"
} : aggregate failed :
example@sqldat.com/mongo/shell/utils.js:25:13
example@sqldat.com/mongo/shell/assert.js:18:14
example@sqldat.com/mongo/shell/assert.js:618:17
example@sqldat.com/mongo/shell/assert.js:708:16
example@sqldat.com/mongo/shell/db.js:266:5
example@sqldat.com/mongo/shell/collection.js:1046:12
@(shell):1:1 ダブルから日付を引くことができないというエラーが表示されます。
2つの日付の差を返す
両方の引数が日付の場合、$subtract 演算子は、2つの日付の差をミリ秒単位で返します。
例:
db.data.aggregate(
[
{ $project: {
_id: 0,
start: 1,
end: 1,
result: {
$subtract: [ "$end", "$start" ] } }
}
]
).pretty() 結果:
{
"start" : ISODate("2021-01-03T00:00:00Z"),
"end" : ISODate("2021-01-03T23:30:15.100Z"),
"result" : NumberLong(84615100)
} 日付を入れ替えると、結果は負の値になります:
db.data.aggregate(
[
{ $project: {
_id: 0,
start: 1,
end: 1,
result: {
$subtract: [ "$start", "$end" ] } }
}
]
).pretty() 結果:
{
"start" : ISODate("2021-01-03T00:00:00Z"),
"end" : ISODate("2021-01-03T23:30:15.100Z"),
"result" : NumberLong(-84615100)
} 間違った数の引数を渡す
$subtract 演算子は正確に2つの引数を受け入れます。間違った数の引数を渡すと、エラーが発生します。
例:
db.data.aggregate(
[
{ $project: {
result: {
$subtract: [ "$a" ] } }
}
]
) 結果:
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "Invalid $project :: caused by :: Expression $subtract takes exactly 2 arguments. 1 were passed in.",
"code" : 16020,
"codeName" : "Location16020"
} : aggregate failed :
example@sqldat.com/mongo/shell/utils.js:25:13
example@sqldat.com/mongo/shell/assert.js:18:14
example@sqldat.com/mongo/shell/assert.js:618:17
example@sqldat.com/mongo/shell/assert.js:708:16
example@sqldat.com/mongo/shell/db.js:266:5
example@sqldat.com/mongo/shell/collection.js:1046:12
@(shell):1:1 ヌル値の受け渡し
2つのnullを渡す 値はnullになります 返送されます。
例:
db.data.aggregate(
[
{ $project: {
result: {
$subtract: [ null, null ] } }
}
]
) 結果:
{ "_id" : 1, "result" : null }