MongoDBでは、$filter 集計パイプライン演算子は、指定された条件に基づいて配列のサブセットを返します。
$filter 演算子は、条件に一致する要素のみを含む配列を元の順序で返します。
構文
構文は次のようになります:
{ $filter: { input: <array>, as: <string>, cond: <expression> } } 各フィールドは以下のとおりです。
| フィールド | 仕様 |
|---|---|
input | 配列に解決される式。 |
as | これはオプションのフィールドです。 inputの個々の要素を表す変数の名前を指定します 配列。名前が指定されていない場合(つまり、このフィールドを省略した場合)、変数名はデフォルトでthisになります 。 |
cond | 要素を出力配列に含める必要があるかどうかを判断するために使用されるブール値に解決される式。この式は、inputの各要素を参照します asで指定された変数名で個別に配列 。 |
例
playersというコレクションがあるとします。 次のドキュメントを使用
{ "_id" : 1, "player" : "Homer", "scores" : [ 1, 5, 3 ] }
{ "_id" : 2, "player" : "Marge", "scores" : [ 8, 17, 18 ] }
{ "_id" : 3, "player" : "Bart", "scores" : [ 15, 11, 8 ] }
$filterを適用する例を次に示します。 scoresの配列要素をフィルタリングする演算子 フィールド:
db.players.aggregate([
{
$match: { _id: { $in: [ 1, 2, 3 ] } }
},
{
$project: {
highScores: {
$filter: {
input: "$scores",
as: "score",
cond: { $gt: [ "$$score", 10 ] }
}
}
}
}
]) 結果:
{ "_id" : 1, "highScores" : [ ] }
{ "_id" : 2, "highScores" : [ 17, 18 ] }
{ "_id" : 3, "highScores" : [ 15, 11 ] } この例では、配列を10より大きい値を持つ要素のみにフィルター処理しました。これらの値のみが返されます。
10未満の値は、結果から省略されます。最初のドキュメントの場合、これにより配列が空になります。
ここでは、asを使用しました 戻り変数に名前を付けるフィールドscore 。次に、condでその変数を参照しました $$scoreを使用するフィールド 。前述のように、asは省略できます。 フィールドをクリックし、$$thisを使用して戻り変数を参照します 。これについては後で詳しく説明します。
空のアレイ
配列が空の場合、空の配列が返されます。
コレクションに次のドキュメントがあるとします。
{ "_id" : 4, "player" : "Farnsworth", "scores" : [ ] }
$filterを適用すると次のようになります その配列に:
db.players.aggregate([
{
$match: { _id: { $in: [ 4 ] } }
},
{
$project: {
highScores: {
$filter: {
input: "$scores",
as: "score",
cond: { $gt: [ "$$score", 10 ] }
}
}
}
}
]) 結果:
{ "_id" : 4, "highScores" : [ ] } 間違ったタイプ
$filterを適用する 配列を含まないフィールドに移動すると、エラーが返されます。
例:
db.players.aggregate([
{
$match: { _id: { $in: [ 4 ] } }
},
{
$project: {
highScores: {
$filter: {
input: "$player",
as: "player",
cond: { $gt: [ "$$player", 10 ] }
}
}
}
}
]) 結果:
Error: command failed: {
"ok" : 0,
"errmsg" : "input to $filter must be an array not string",
"code" : 28651,
"codeName" : "Location28651"
} : 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 ヌル値
フィールドにnullが含まれている場合 配列の代わりに、結果はnullになります 。
コレクションに次のドキュメントがあるとします。
{ "_id" : 5, "player" : "Meg", "scores" : null }
$filterを適用すると次のようになります scoresに フィールド:
db.players.aggregate([
{
$match: { _id: { $in: [ 5 ] } }
},
{
$project: {
highScores: {
$filter: {
input: "$scores",
as: "score",
cond: { $gt: [ "$$score", 10 ] }
}
}
}
}
]) 結果:
{ "_id" : 5, "highScores" : null } 存在しないフィールド
$filterを適用する 存在しないフィールドに移動すると、nullになります 返送されます。
例:
db.players.aggregate([
{
$match: { _id: { $in: [ 5 ] } }
},
{
$project: {
highScores: {
$filter: {
input: "$name",
as: "name",
cond: { $gt: [ "$$name", 10 ] }
}
}
}
}
]) 結果:
{ "_id" : 5, "highScores" : null } 変数名はオプションです
前の例では、asを使用しています 変数に名前を割り当てるフィールド。
前述のように、as フィールドはオプションです。このフィールドを省略すると、変数名はデフォルトでthisになります 。
次に例を示します:
db.players.aggregate([
{
$match: { _id: { $in: [ 1, 2, 3 ] } }
},
{
$project: {
highScores: {
$filter: {
input: "$scores",
cond: { $gt: [ "$$this", 10 ] }
}
}
}
}
])
結果:
{ "_id" : 1, "highScores" : [ ] }
{ "_id" : 2, "highScores" : [ 17, 18 ] }
{ "_id" : 3, "highScores" : [ 15, 11 ] }
これは最初の例と同じですが、この例ではasを省略しています。 フィールドであるため、$$thisを使用して変数を参照します 。