Welcome back to the MongoDB learning series!
Today, we explore powerful array query operators $in and $elemMatch. These are essential for querying documents with arrays and multi-criteria matches, an important skill for the MongoDB Associate Developer exam and real-world tasks.
Arrays are fundamental BSON types in MongoDB. Querying array fields requires special operators beyond simple equality.
$in operator matches documents where a field’s value equals any in a specified list. It’s useful for checking if any values from a list are present in a field.$elemMatch operator is used to find documents where at least one element in an array satisfies multiple conditions, often involving compound criteria.$in Operator{ field: { $in: [value1, value2, ...] } }
field is equal to any value in the provided array.$elemMatch Operator{ arrayField: { $elemMatch: { condition1, condition2, ... } } }
arrayField satisfies all specified conditions.db.movies.find({ genres: { $in: ["Drama", "Comedy"] } })
This finds movies where the genres array contains at least one of "Drama" or "Comedy".
db.movies.find({
genres: { $elemMatch: { $eq: "Action" } },
"imdb.rating": { $gt: 8 }
})
This finds movies having "Action" in their genres and an IMDb rating greater than 8.
Note:
Here $elemMatch enables matching array elements cleanly. If querying embedded documents arrays, you can specify multiple conditions inside $elemMatch.
from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017/')
db = client.sample_mflix
cursor = db.movies.find({ "genres": { "$in": ["Drama", "Comedy"] } })
for movie in cursor:
print(movie["title"], movie.get("genres"))
$in behaves like an OR for specified values, easy for simple array inclusion.$elemMatch allows specifying multiple conditions for the same array element.$elemMatch is necessary when matching complex array of subdocuments or conditional queries.$in and $elemMatch for layered queries.Q1: What does $in operator in query do?
a) Matches documents where field contains all values in array
b) Matches documents where field matches any value in array
c) Matches documents where field is equal to the first array element
d) Matches documents with arrays larger than specified array
Q2: How does $elemMatch differ from $in when querying arrays?
a) $elemMatch matches multiple conditions in a single element
b) $elemMatch matches any element in an array with listed values
c) $elemMatch replaces $in functionality
d) $elemMatch only works with numeric arrays
Q3: Given the query:
db.movies.find({ genres: { $elemMatch: { $eq: "Action" } } })
What movies will match?
a) Movies with "Action" as one genre in genres array
b) Movies with "Action" as all genres
c) Movies where all genres match exactly "Action"
d) No movies, query is invalid