books with specific book documents$in to find books matching those genresJump into concepts and practice - no test required
books with specific book documents$in to find books matching those genresbooks collection with sample databooks and insert these exact documents: { title: "The Hobbit", genre: "Fantasy" }, { title: "1984", genre: "Dystopian" }, { title: "To Kill a Mockingbird", genre: "Classic" }, { title: "The Great Gatsby", genre: "Classic" }, { title: "Neuromancer", genre: "Science Fiction" }.Use db.books.insertMany([...]) with the exact documents listed.
searchGenres and assign it the array ["Classic", "Science Fiction"].Use const searchGenres = ["Classic", "Science Fiction"] to define the array.
$inquery that uses { genre: { $in: searchGenres } } to find books whose genre is in the searchGenres array.Use const query = { genre: { $in: searchGenres } } to create the query.
db.books.find(query) to find all books matching the genres in searchGenres.Use db.books.find(query) to retrieve the matching documents.
What does the $in operator do in MongoDB queries?
$in$in operator is used to match documents where a field's value is equal to any value in a specified array.$in.$in matches any value in list [OK]$in checks if field is in a list [OK]$in with comparison operators like $gt$in updates or deletes documents$in without an array of valuesWhich of the following is the correct syntax to find documents where the field color is either "red", "blue", or "green"?
{ color: { $in: ["red", "blue", "green"] } }$in syntax$in operator requires an array of values inside square brackets []. { color: { $in: ["red", "blue", "green"] } } correctly uses an array with strings.$in needs an array [OK]$in values [OK]$inGiven the collection products with documents:
[{ "name": "Pen", "category": "stationery" }, { "name": "Apple", "category": "fruit" }, { "name": "Notebook", "category": "stationery" }, { "name": "Banana", "category": "fruit" }]What will be the result of this query?
db.products.find({ category: { $in: ["fruit"] } })category field is in the array ["fruit"]. This means only documents with category "fruit" will match.What is wrong with this MongoDB query?
db.users.find({ age: { $in: 25, 30, 35 } })$in usage$in operator expects a single array argument containing values to match. Here, values are passed as separate arguments, which is invalid syntax.{ age: { $in: [25, 30, 35] } } with square brackets around the values.$in operator requires an array of values, not separate arguments -> Option C$in needs an array [OK]$in values in square brackets [OK]$in with other operatorsYou have a collection orders with documents containing a field status. You want to find all orders where the status is either "pending", "shipped", or "delivered" but exclude those with status "cancelled" or "returned". Which query correctly uses $in and $nin to achieve this?
$in and $nin on the same fieldstatus is in one list but not in another. MongoDB does not allow combining $in and $nin inside the same object for a field. Instead, both conditions must be combined using an implicit AND by specifying both in the query object.$or, which applies OR logic, not the required AND. { status: { $in: ["pending", "shipped", "delivered"] }, $nin: ["cancelled", "returned"] } places $nin outside the field, which is invalid. { status: { $in: ["pending", "shipped", "delivered"], $nin: ["cancelled", "returned"] } } tries to combine $in and $nin inside the same object, which is invalid in MongoDB.$and or by specifying both operators inside an object using $and or by using the implicit AND by combining conditions in the query object like: { status: { $in: [...] }, status: { $nin: [...] } } is invalid JSON because keys repeat. So the correct query is:{ status: { $in: [...] }, status: { $nin: [...] } } is invalid.{ status: { $in: [...] }, status: { $nin: [...] } } is invalid.{ status: { $in: [...] }, $nin: [...] } is invalid.{ status: { $in: [...] }, $nin: [...] } is invalid.$and operator:{ $and: [ { status: { $in: [...] } }, { status: { $nin: [...] } } ] }$and to combine $in and $nin conditions [OK]$nin outside the field object