MongoDBでは、$range 集計パイプライン演算子は、生成された数列を配列で返します。
この一連の数字は、指定した入力値に基づいています。
構文
構文は次のようになります:
{ $range: [ <start>, <end>, <non-zero step> ] }
<start>の場所 は開始であり、<end> シーケンスの終わりです。これらはそれぞれ、整数に解決される任意の有効な式にすることができます。
<non-zero step> はオプションの引数で、デフォルトは1です。この引数を使用すると、増分値を指定できます。指定する場合は、ゼロ以外の整数に解決される有効な式である必要があります。
例
rangeというコレクションがあるとします。 次のドキュメントを使用:
{ "_id" : 1, "start" : 0, "end" : 5 }
{ "_id" : 2, "start" : 1, "end" : 5 }
$rangeを使用できます それらのドキュメントの値に基づいて配列を返す演算子。
db.range.aggregate(
[
{ $match: { _id: { $in: [ 1, 2 ] } } },
{
$project:
{
_id: 0,
start: 1,
end: 1,
result: { $range: [ "$start", "$end" ] }
}
}
]
) 結果:
{ "start" : 0, "end" : 5, "result" : [ 0, 1, 2, 3, 4 ] }
{ "start" : 1, "end" : 5, "result" : [ 1, 2, 3, 4 ] }
この場合、3番目の引数を提供しなかったため、$range デフォルトのステップ値1を使用します。したがって、配列要素は1ずつインクリメントします。
明示的な増分を追加する
3番目の引数を追加して、各配列要素をどれだけインクリメントするかを明示的に指定できます。
コレクションに次のドキュメントが含まれているとします。
{ "_id" : 3, "start" : 0, "end" : 5, "step" : 1 }
{ "_id" : 4, "start" : 0, "end" : 10, "step" : 2 }
{ "_id" : 5, "start" : 1, "end" : 10, "step" : 2 }
{ "_id" : 6, "start" : 100, "end" : 150, "step" : 10 }
これらのドキュメントにはstepがあります フィールドなので、そのフィールドをそれぞれのドキュメントの増分値に使用できます。
それでは、$rangeを適用しましょう それらのドキュメントに、stepを含めます 3番目の引数としてのフィールド:
db.range.aggregate(
[
{ $match: { _id: { $in: [ 3, 4, 5, 6 ] } } },
{
$project:
{
_id: 0,
start: 1,
end: 1,
step: 1,
result: { $range: [ "$start", "$end", "$step" ] }
}
}
]
) 結果:
{ "start" : 0, "end" : 5, "step" : 1, "result" : [ 0, 1, 2, 3, 4 ] }
{ "start" : 0, "end" : 10, "step" : 2, "result" : [ 0, 2, 4, 6, 8 ] }
{ "start" : 1, "end" : 10, "step" : 2, "result" : [ 1, 3, 5, 7, 9 ] }
{ "start" : 100, "end" : 150, "step" : 10, "result" : [ 100, 110, 120, 130, 140 ] } 負のステップ値
ステップは負の値になる可能性がありますが、これは、より高いstartからデクリメントするコンテキストで実行する必要があります。 下位のendへの番号 番号。
コレクションにさらにいくつかのドキュメントを追加しましょう:
{ "_id" : 7, "start" : 0, "end" : 5, "step" : -1 }
{ "_id" : 8, "start" : 5, "end" : 0, "step" : -1 }
{ "_id" : 9, "start" : 0, "end" : -5, "step" : -1 }
それでは、$rangeを適用しましょう それらのドキュメントへ:
db.range.aggregate(
[
{ $match: { _id: { $in: [ 7, 8, 9 ] } } },
{
$project:
{
_id: 0,
start: 1,
end: 1,
step: 1,
result: { $range: [ "$start", "$end", "$step" ] }
}
}
]
) 結果:
{ "start" : 0, "end" : 5, "step" : -1, "result" : [ ] }
{ "start" : 5, "end" : 0, "step" : -1, "result" : [ 5, 4, 3, 2, 1 ] }
{ "start" : 0, "end" : -5, "step" : -1, "result" : [ 0, -1, -2, -3, -4 ] }
負のステップ値がstartによって提供される範囲外であるため、最初のドキュメントが空の配列を返したことがわかります。 およびend フィールド。
ただし、後続のドキュメントでは、値の範囲が減少しました。
ステップがゼロの場合
ステップ値はゼロ以外の整数でなければなりません。 0のステップを提供する エラーを返します。
コレクションに次のドキュメントを追加するとします。
{ "_id" : 10, "start" : 1, "end" : 5, "step" : 0 }
$rangeを適用すると次のようになります そのドキュメントへ:
db.range.aggregate(
[
{ $match: { _id: { $in: [ 10 ] } } },
{
$project:
{
_id: 0,
start: 1,
end: 1,
result: { $range: [ "$start", "$end", "$step" ] }
}
}
]
) 結果:
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "$range requires a non-zero step value",
"code" : 34449,
"codeName" : "Location34449"
} : 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:639:17
example@sqldat.com/mongo/shell/assert.js:729:16
example@sqldat.com/mongo/shell/db.js:266:5
example@sqldat.com/mongo/shell/collection.js:1058:12
@(shell):1:1
エラーメッセージは、$range requires a non-zero step value 。
ヌルステップ
ステップをnullにすることはできません どちらか。
次のドキュメントがあるとします。
{ "_id" : 11, "start" : 1, "end" : 5, "step" : null }
そして、$rangeを適用します それに:
db.range.aggregate(
[
{ $match: { _id: { $in: [ 11 ] } } },
{
$project:
{
_id: 0,
start: 1,
end: 1,
result: { $range: [ "$start", "$end", "$step" ] }
}
}
]
) 結果:
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "$range requires a numeric step value, found value of type:null",
"code" : 34447,
"codeName" : "Location34447"
} : 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:639:17
example@sqldat.com/mongo/shell/assert.js:729:16
example@sqldat.com/mongo/shell/db.js:266:5
example@sqldat.com/mongo/shell/collection.js:1058:12
@(shell):1:1
これは、$range requires a numeric step value, found value of type:nullの値が見つかったことを示しています。 。
ヌル範囲
startの場合 および/またはend フィールドはnull 、その後、エラーが返されます。
次のドキュメントがあるとします。
{ "_id" : 11, "start" : 1, "end" : 5, "step" : null }
そして、$rangeを適用します それに:
db.range.aggregate(
[
{ $match: { _id: { $in: [ 11 ] } } },
{
$project:
{
_id: 0,
start: 1,
end: 1,
result: { $range: [ "$start", "$end", "$step" ] }
}
}
]
) 結果:
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "$range requires a numeric starting value, found value of type: null",
"code" : 34443,
"codeName" : "Location34443"
} : 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:639:17
example@sqldat.com/mongo/shell/assert.js:729:16
example@sqldat.com/mongo/shell/db.js:266:5
DBCoexample@sqldat.com/mongo/shell/collection.js:1058:12
@(shell):1:1
これは、$range requires a numeric starting value, found value of type: null 。
終了値がnullの場合も、同様のメッセージが表示されます。
これがnullのドキュメントです 最終値:
{ "_id" : 13, "start" : 1, "end" : null, "step" : 1 }
$rangeを適用しましょう :
db.range.aggregate(
[
{ $match: { _id: { $in: [ 13 ] } } },
{
$project:
{
_id: 0,
start: 1,
end: 1,
result: { $range: [ "$start", "$end", "$step" ] }
}
}
]
) 結果:
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "$range requires a numeric ending value, found value of type: null",
"code" : 34445,
"codeName" : "Location34445"
} : 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:639:17
example@sqldat.com/mongo/shell/assert.js:729:16
example@sqldat.com/mongo/shell/db.js:266:5
example@sqldat.com/mongo/shell/collection.js:1058:12
@(shell):1:1
今回は、$range requires a numeric ending value, found value of type: nullです。 。