MongoDB Query Master: The Ultimate Filtering Quiz Quiz

  1. Finding Documents by Field Value

    What is the correct MongoDB query to find all documents where the 'status' field is equal to 'active'?

    1. { status: 'active' }
    2. db.collection.find({ status: 'active' })
    3. db.collection.find('status': 'active')
    4. db.collection.findOne({ status: 'active' })
    5. {'status': 'actiev'}
  2. Using the $gt Operator

    Which operator is used in MongoDB to find documents where a field's value is greater than a specified value?

    1. $lt
    2. $gte
    3. $eq
    4. $gt
    5. $lte
  3. Filtering with $in

    How do you find documents where the 'category' field is either 'books' or 'movies' using the $in operator?

    1. db.collection.find({ category: { $in: ['books', 'movies'] } })
    2. db.collection.find({ category: ['$in', 'books', 'movies'] })
    3. db.collection.find({ category: { $eq: ['books', 'movies'] } })
    4. db.collection.find({ category: { $all: ['books', 'movies'] } })
    5. db.collection.find({ category: { $in: 'books', 'movies' } })
  4. Using $and Operator

    What is the correct way to use the $and operator to find documents where 'age' is greater than 25 AND 'city' is 'New York'?

    1. db.collection.find({ $and: [ { age: { $gt: 25 } }, { city: 'New York' } ] })
    2. db.collection.find({ age: { $gt: 25 }, city: 'New York' })
    3. db.collection.find({ $and: { age: { $gt: 25 }, city: 'New York' } })
    4. db.collection.find({ age: $gt(25) AND city: 'New York' })
    5. db.collection.find({ $and: [ age: { $gt: 25 }, city: 'New York' ] })
  5. The $exists Operator

    How would you find documents where the field 'email' exists?

    1. db.collection.find({ email: { $exists: true } })
    2. db.collection.find({ email: { $exists: 1 } })
    3. db.collection.find({ email: $exists(true) })
    4. db.collection.find({ $exists: 'email' })
    5. db.collection.find({ email: { $exists: 'true' } })
  6. Filtering Arrays with $elemMatch

    Which operator is used to filter documents where an array field contains at least one element that matches all specified criteria?

    1. $all
    2. $in
    3. $elemMatch
    4. $size
    5. $contains
  7. Using $not with $regex

    How do you find documents where the 'name' field does NOT contain the word 'test' using $not and $regex?

    1. db.collection.find({ name: { $not: /test/ } })
    2. db.collection.find({ name: { $regex: 'test', $not: true } })
    3. db.collection.find({ name: { $not: { $regex: 'test' } } })
    4. db.collection.find({ $not: { name: { $regex: 'test' } } })
    5. db.collection.find({name: {$regex: '^((?!test).)*$'}})
  8. Limiting and Skipping Results

    How would you retrieve the first 5 documents and skip the initial 10 in a collection, using MongoDB's limit and skip methods?

    1. db.collection.find().limit(5).skip(10)
    2. db.collection.find().skip(10).limit(5)
    3. db.collection.limit(5).skip(10).find()
    4. db.collection.find().limit(10).skip(5)
    5. db.collection.find().skip(5).limit(10)
  9. Querying with Regular Expressions

    What is the correct syntax to find documents where the 'city' field starts with the letter 'N' using a regular expression?

    1. db.collection.find({ city: /^N/ })
    2. db.collection.find({ city: { $regex: 'N*' } })
    3. db.collection.find({ city: { $startswith: 'N' } })
    4. db.collection.find({ city: { $regex: 'N' } })
    5. db.collection.find({ city: {$regex: '^[N]'}})
  10. Using $or Operator

    How to find documents where either 'quantity' is less than 10 OR 'price' is greater than 100?

    1. db.collection.find({ $or: [ { quantity: { $lt: 10 } }, { price: { $gt: 100 } } ] })
    2. db.collection.find({ quantity: { $lt: 10 } || price: { $gt: 100 } })
    3. db.collection.find({ $or: { quantity: { $lt: 10 }, price: { $gt: 100 } } })
    4. db.collection.find({ quantity: { $lt: 10 }, $or: { price: { $gt: 100 } } })
    5. db.collection.find({ or: [ { quantity: { $lt: 10 } }, { price: { $gt: 100 } } ] })