互換性
次の環境でホストされる配置には $in を使用できます。
MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
構文
$in演算子は次のような形をとります。
{ field: { $in: [ <value1>, <value2>, ... <valueN> ] } }
異なる BSON 型値の比較については、BSON 比較順序の指定を参照してください。
field に配列がある場合、$in 演算子は、field が指定された配列内の値と一致する要素を少なくとも 1 つ含む配列を持つドキュメントを選択します。例、、、<value1>、<value2>など。
$in は各パラメータをコレクション内の各ドキュメントと比較するため、パフォーマンスの問題が発生する可能性があります。パフォーマンスを向上させるには、クエリを実行する field にインデックスを作成します。インデックスを使用すると、 MongoDB は各 $in 要素の限界を作成し、より効率的に検索できます。
注意
このドキュメントでは、$in クエリ演算子について説明します。$in 集計演算子については、 $in(式演算子) を参照してください。
MongoDB Search を使用した Atlas 上のデータのクエリ
MongoDB Atlasに保存されているデータには、 クエリを実行中中にMongoDB Search イン参照 $search演算子を使用できます。 の後に を実行するのは、$in $search$searchinref 演算子とともに を実行中よりパフォーマンスが低くなります。
この演算子のMongoDB Search バージョンの詳細については、Atlas ドキュメントの inref 演算子を参照してください。
例
このページの例では、 sample_mflixサンプルデータセット のデータを使用します。このデータセットを自己管理型MongoDBデプロイにロードする 方法の詳細については、「サンプルデータセットをロードする 」を参照してください。サンプルデータベースに変更を加えた場合、このページの例を実行するには、データベースを削除して再作成する必要がある場合があります。
Match Values
このクエリでは、movies コレクションの rated フィールドの値が "G" または "TV-G" であるドキュメントを選択します。
db.movies.find( { rated: { $in: ["G", "TV-G"] } }, { _id: 0, title: 1 } )
[ { title: 'The Great Train Robbery' }, { title: 'A Corner in Wheat' }, { title: 'From Hand to Mouth' }, { title: 'One Week' }, { title: 'The Devil to Pay!' }, { title: 'Footlight Parade' }, { title: 'Gold Diggers of 1935' }, { title: 'Naughty Marietta' }, { title: 'Modern Times' }, { title: 'Gone with the Wind' }, { title: 'Fantasia' }, { title: 'The Man Who Came to Dinner' }, { title: 'National Velvet' }, { title: 'Alice in Wonderland' }, { title: 'The Member of the Wedding' }, { title: 'Seven Brides for Seven Brothers' }, { title: 'Around the World in Eighty Days' }, { title: 'The King and I' }, { title: 'A King in New York' }, { title: 'Ben-Hur' } ]
$or演算子を使用してクエリを作成できますが、同じフィールドで等価性チェックを実行する場合は、$in 演算子ではなく$or 演算子を使用します。
配列の値の一致
次の updateMany() 操作では、rated 配列に "G" または "TV-G" のいずれかに一致する要素が少なくとも1つある場合、familyFriendly フィールドが true に設定されます。
db.movies.updateMany( { rated: { $in: ["G", "TV-G"] } }, { $set: { familyFriendly: true } } )
{ acknowledged: true, insertedId: null, matchedCount: 536, modifiedCount: 536, upsertedCount: 0 }
配列のクエリに関するその他の例えは、以下を参照してください。
クエリに関するその他の例えについては、「ドキュメントのクエリ 」を参照してください。
$in を正規表現で使用する
$in 演算子は、/pattern/ 形式の正規表現を使用してドキュメントを選択できます。
このクエリは、plotフィールドがAlien で始まる、 または sci-fi を含む moviesコレクション内のドキュメントを選択します。
db.movies.find( { plot: { $in: [ /^Alien/ , /sci-fi/ ] } }, { _id: 0, title: 1, plot: 1 } )
[ { plot: 'Aliens come to Earth seeking scientists to help them in their war.', title: 'This Island Earth' }, { plot: 'Censored by the Polish authorities, this film was reedited and new footage added. It begins with a sci-fi motif: abstract images and electronic music take the viewer from ruins of Lebanon ...', title: 'Rece do gèry' }, { plot: 'An idyllic sci-fi future has one major drawback: life must end at 30.', title: "Logan's Run" }, { plot: "Four horror/sci-fi segments directed by four famous directors which are their own versions of classic stories from Rod Serling's landmark television series.", title: 'Twilight Zone: The Movie' }, { plot: 'Aliens who look like clowns come from outer space and terrorize a small town.', title: 'Killer Klowns from Outer Space' }, ... ]