MongoDBでは、$in 集計パイプライン演算子は、指定された値が配列にあるかどうかを示すブール値を返します。
$in 集計パイプライン演算子を$inと混同しないでください クエリ演算子。フィールドの値が指定された配列の任意の値と等しいドキュメントを選択します。
例
productsというコレクションがあるとします。 次のドキュメントを使用:
{ "_id" : 1, "prod" : "Bat", "sizes" : [ "S", "M", "XL", "XXL" ] }
{ "_id" : 2, "prod" : "Hat", "sizes" : [ "S", "L", "XL" ] }
{ "_id" : 3, "prod" : "Cap", "sizes" : [ "XS", "S", "M", "L", "XL" ] }
$inを使用できます sizesかどうかを確認する演算子 フィールドには値Lが含まれます 。
例:
db.products.aggregate(
[
{
$project:
{
hasLarge: { $in: [ "L", "$sizes" ] }
}
}
]
) 結果:
{ "_id" : 1, "hasLarge" : false }
{ "_id" : 2, "hasLarge" : true }
{ "_id" : 3, "hasLarge" : true } 2番目の引数は配列に解決する必要があります
$inの2番目の引数 演算子は配列に解決する必要があります。そうでない場合は、エラーが返されます。
次のドキュメントをコレクションに追加するとします。
{ "_id" : 4, "prod" : "Shirt", "sizes" : "L" }
sizesに注意してください フィールドは配列ではなく、文字列です。
$inを適用しましょう そのドキュメントへ:
db.products.aggregate(
[
{ $match: { _id: { $in: [ 4 ] } } },
{
$project:
{
hasLarge: { $in: [ "L", "$sizes" ] }
}
}
]
) 結果:
Error: command failed: {
"ok" : 0,
"errmsg" : "$in requires an array as a second argument, found: string",
"code" : 40081,
"codeName" : "Location40081"
} : 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
この場合、sizes フィールドに文字列が含まれているため、エラーが返されます。
この例では、$inも使用しています $matchを使用 ドキュメントを私が興味のあるものだけにフィルタリングするための演算子。これは$inを使用することに注意してください。 クエリ演算子の構文。単一の引数を受け入れ、2つの引数の構文と混同しないでください。
ヌル値
2番目の引数がnullの場合も、同じことが起こります。 。
次のドキュメントをコレクションに追加しましょう:
{ "_id" : 5, "prod" : "Jeans", "sizes" : null }
$inを使おうとするとどうなりますか そのドキュメントで:
db.products.aggregate(
[
{ $match: { _id: { $in: [ 5 ] } } },
{
$project:
{
hasLarge: { $in: [ "L", "$sizes" ] }
}
}
]
) 結果:
Error: command failed: {
"ok" : 0,
"errmsg" : "$in requires an array as a second argument, found: null",
"code" : 40081,
"codeName" : "Location40081"
} : 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
DB.prototype._runAggregaexample@sqldat.com/mongo/shell/db.js:266:5
example@sqldat.com/mongo/shell/collection.js:1046:12
@(shell):1:1 同じエラー。
欠落しているフィールド
不足しているフィールドについても同じです。
コレクションに次のドキュメントを追加するとします。
{ "_id" : 6, "prod" : "Shorts" }
そして今、$inを適用しようとしています (存在しない)sizesに フィールド:
db.products.aggregate(
[
{ $match: { _id: { $in: [ 6 ] } } },
{
$project:
{
hasLarge: { $in: [ "L", "$sizes" ] }
}
}
]
) 結果:
Error: command failed: {
"ok" : 0,
"errmsg" : "$in requires an array as a second argument, found: missing",
"code" : 40081,
"codeName" : "Location40081"
} : 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 正規表現
$inとは異なり クエリ演算子、$in 集約パイプライン演算子は、正規表現の使用をサポートしていません。
したがって、次のコードは一致しません。
db.products.aggregate(
[
{ $match: { _id: { $in: [ 1, 2, 3 ] } } },
{
$project:
{
hasLarge: { $in: [ /^X/, "$sizes" ] }
}
}
]
) 結果:
{ "_id" : 1, "hasLarge" : false }
{ "_id" : 2, "hasLarge" : false }
{ "_id" : 3, "hasLarge" : false }
すべてのドキュメントの結果はfalseです。 、sizes 3つのドキュメントすべてのフィールドには、大文字のXで始まる配列要素が含まれています。 。