MCQs Of Day 14: MongoDB Query Operators
1. Which operator is used to find documents where the value of a field is greater than a specified value?
a) $lt
b) $gt
c) $in
d) $ne
Answer: b) $gt
Explanation: The $gt operator is used to select documents where the value of a field is greater than a specified value.
2. Which of the following operators is used to find documents where the value of a field is less than a specified value?
a) $eq
b) $lt
c) $gt
d) $in
Answer: b) $lt
Explanation: The $lt operator is used to select documents where the value of a field is less than the specified value.
3. Which operator is used to check if a field contains a value from a list?
a) $gt
b) $lt
c) $in
d) $exists
Answer: c) $in
Explanation: The $in operator is used to match documents where the field's value is in the given array.
4. Which operator is used to match documents where a field's value does not equal a specified value?
a) $eq
b) $ne
c) $in
d) $gt
Answer: b) $ne
Explanation: The $ne operator selects documents where the field's value is not equal to the specified value.
5. Which operator can be used to check if a field exists in a document?
a) $eq
b) $exists
c) $regex
d) $in
Answer: b) $exists
Explanation: The $exists operator checks whether a specific field exists in the document.
6. What does the $regex operator do in MongoDB?
a) Compares two values for equality
b) Matches a field to a specified regular expression
c) Ensures that a field is unique
d) Checks whether a field contains a specific string
Answer: b) Matches a field to a specified regular expression
Explanation: The $regex operator is used to find documents where a field matches a specified regular expression pattern.
7. Which operator would you use to match documents where the field value is greater than or equal to a specified value?
a) $gt
b) $gte
c) $lt
d) $lte
Answer: b) $gte
Explanation: The $gte operator is used to find documents where the field's value is greater than or equal to the specified value.
8. To find students whose name starts with the letter 'A', which operator is used?
a) $in
b) $exists
c) $regex
d) $eq
Answer: c) $regex
Explanation: The $regex operator is used for pattern matching, and ^A matches names starting with "A".
9. What is the correct way to check if a student has a scholarship field in MongoDB?
a) { scholarship: true }
b) { scholarship: { $exists: true } }
c) { scholarship: { $exists: false } }
d) { scholarship: { $eq: true } }
Answer: b) { scholarship: { $exists: true } }
Explanation: The $exists: true operator checks if the field scholarship exists in the document.
10. Which operator would you use to find documents where a field is NOT in a specific list of values?
a) $notIn
b) $ne
c) $nin
d) $exists
Answer: c) $nin
Explanation: The $nin operator is used to find documents where the field’s value is not in the specified list.
11. What would the query db.students.find({ age: { $gt: 25 } }) return?
a) All students older than 25
b) All students under 25
c) Students who are exactly 25 years old
d) Students who are younger than 25
Answer: a) All students older than 25
Explanation: This query uses the $gt operator, which returns documents where the age field is greater than 25.
12. Which of the following is used to perform a pattern match in MongoDB?
a) $exists
b) $regex
c) $gt
d) $lt
Answer: b) $regex
Explanation: The $regex operator allows you to perform pattern matching on string fields in MongoDB.
13. Which operator checks for equality between two fields?
a) $eq
b) $ne
c) $gt
d) $lt
Answer: a) $eq
Explanation: The $eq operator checks if two values are equal.
14. Which operator would you use to find students who have a name equal to "Alice"?
a) { name: { $gt: "Alice" } }
b) { name: "Alice" }
c) { name: { $eq: "Alice" } }
d) { name: { $ne: "Alice" } }
Answer: b) { name: "Alice" }
Explanation: In MongoDB, you can simply query with { name: "Alice" } to find documents where the name is exactly "Alice."
15. To find all students who are either "Computer Science" or "Mathematics" majors, which operator is used?
a) $gt
b) $in
c) $ne
d) $exists
Answer: b) $in
Explanation: The $in operator is used to find documents where the major field is one of the values in the specified list.
16. Which query will find all students whose name starts with "J"?
a) { name: { $regex: /J/ } }
b) { name: { $regex: /^J/ } }
c) { name: { $gt: "J" } }
d) { name: { $ne: "J" } }
Answer: b) { name: { $regex: /^J/ } }
Explanation: The $regex operator with ^J matches names starting with "J".
17. What does the query db.students.find({ age: { $lte: 22 } }) return?
a) All students older than 22
b) All students younger than 22
c) All students aged 22 or less
d) All students aged exactly 22
Answer: c) All students aged 22 or less
Explanation: The $lte operator selects documents where the field value is less than or equal to 22.
18. What is the result of using $exists: false in a query?
a) It selects documents where the field exists.
b) It selects documents where the field does not exist.
c) It checks if a value is null.
d) It checks for an undefined field.
Answer: b) It selects documents where the field does not exist.
Explanation: The $exists: false operator ensures that the field is absent in the documents.
19. Which of the following operators is used for performing greater-than comparisons in MongoDB?
a) $lt
b) $gt
c) $eq
d) $exists
Answer: b) $gt
Explanation: The $gt operator is used to find documents where the value of a field is greater than a specified value.
20. What would the query db.students.find({ name: { $regex: /math/i } }) return?
a) Students whose names contain "math" in any case
b) Students whose names start with "math"
c) Students whose names contain the exact word "math"
d) Students whose names do not contain "math"
Answer: a) Students whose names contain "math" in any case
Explanation: The /math/i regular expression matches any occurrence of "math", case-insensitive.
21. Which operator would be used to find documents where a field contains a value equal to any of the values in a provided list?
a) $gt
b) $eq
c) $in
d) $regex
Answer: c) $in
Explanation: The $in operator matches documents where a field's value is in the provided list.
22. Which query will return all documents where the major field is not "Mathematics"?
a) { major: { $ne: "Mathematics" } }
b) { major: "Mathematics" }
c) { major: { $in: ["Mathematics"] } }
d) { major: { $exists: true } }
Answer: a) { major: { $ne: "Mathematics" } }
Explanation: The $ne operator is used to find documents where the major field is not equal to "Mathematics".
23. What would be the result of db.students.find({ name: { $regex: /^j/ } })?
a) Documents where the name starts with "j"
b) Documents where the name ends with "j"
c) Documents where the name contains "j" anywhere
d) Documents where the name is exactly "j"
Answer: a) Documents where the name starts with "j"
Explanation: The /^j/ regex matches names starting with "j".
24. Which operator allows you to query for a value that matches a pattern, such as a word starting with a specific letter?
a) $exists
b) $regex
c) $in
d) $gt
Answer: b) $regex
Explanation: The $regex operator allows you to perform pattern matching for string fields.
25. What is the correct syntax for checking if the scholarship field is missing in the documents?
a) { scholarship: { $exists: false } }
b) { scholarship: { $exists: true } }
c) { scholarship: null }
d) { scholarship: { $ne: null } }
Answer: a) { scholarship: { $exists: false } }
Explanation: The $exists: false checks for documents where the scholarship field is missing.
26. Which of the following operators is used for performing exact match queries in MongoDB?
a) $eq
b) $gt
c) $lt
d) $regex
Answer: a) $eq
Explanation: The $eq operator performs an exact match query.
27. Which operator allows you to find documents where a field contains a value that matches a specified regular expression?
a) $gt
b) $eq
c) $regex
d) $exists
Answer: c) $regex
Explanation: The $regex operator is used to find documents where a field matches a regular expression.
28. What is the result of using { age: { $lt: 20 } } in a MongoDB query?
a) Find documents where the age field is less than 20
b) Find documents where the age field is greater than 20
c) Find documents where the age field is equal to 20
d) Find documents where the age field is less than or equal to 20
Answer: a) Find documents where the age field is less than 20
Explanation: The $lt operator finds documents where the value of age is less than 20.
29. Which operator is used to find documents where a field value matches a specified regular expression pattern in MongoDB?
a) $exists
b) $in
c) $regex
d) $gt
Answer: c) $regex
Explanation: The $regex operator is used for regular expression pattern matching.
30. What does the query db.students.find({ major: { $in: ["Computer Science", "Mathematics"] } }) return?
a) Documents where the major is either "Computer Science" or "Mathematics"
b) Documents where the major is neither "Computer Science" nor "Mathematics"
c) Documents where the major is "Computer Science" only
d) Documents where the major is "Mathematics" only
Answer: a) Documents where the major is either "Computer Science" or "Mathematics"
Explanation: The $in operator is used to select documents where the major is one of the specified values.
31. Which operator is used to query documents where the value of a field is not equal to a given value?
a) $ne
b) $in
c) $exists
d) $gt
Answer: a) $ne
Explanation: The $ne operator is used to find documents where a field's value is not equal to the specified value.
32. Which query returns all students where the name starts with "J"?
a) { name: { $regex: "J" } }
b) { name: { $regex: /^J/ } }
c) { name: "J*" }
d) { name: "J" }
Answer: b) { name: { $regex: /^J/ } }
Explanation: The $regex operator with ^J matches names starting with "J".
33. Which operator is used to find all documents where a field contains the specified value?
a) $eq
b) $ne
c) $in
d) $exists
Answer: a) $eq
Explanation: The $eq operator finds documents where the field's value is equal to the specified value.
34. Which query can find all students whose age is greater than or equal to 18?
a) { age: { $gt: 18 } }
b) { age: { $lt: 18 } }
c) { age: { $gte: 18 } }
d) { age: { $eq: 18 } }
Answer: c) { age: { $gte: 18 } }
Explanation: The $gte operator is used to find documents where the field's value is greater than or equal to the specified value.
35. Which of the following operators is used to query a field that exists?
a) $eq
b) $exists
c) $gt
d) $lt
Answer: b) $exists
Explanation: The $exists operator checks for the presence of a specified field in the documents.
36. Which query matches documents where the name field does not start with "S"?
a) { name: { $regex: /^S/ } }
b) { name: { $regex: /S$/ } }
c) { name: { $regex: /^S/ } }
d) { name: { $ne: "S" } }
Answer: c) { name: { $regex: /^S/ } }
Explanation: The $regex operator is used to match documents where the name field does not start with the letter "S".
37. Which operator would you use to find students whose names start with "Al"?
a) { name: /^Al/ }
b) { name: /Al/ }
c) { name: /^Al/ }
d) { name: /Al/i }
Answer: c) { name: /^Al/ }
Explanation: The $regex operator with ^Al matches names starting with "Al".
38. Which operator is used to check whether a document contains a field or not?
a) $exists
b) $gt
c) $in
d) $lt
Answer: a) $exists
Explanation: The $exists operator checks for the presence of a field in the document.
39. Which query returns all documents where major is either "Computer Science" or "Engineering"?
a) { major: { $gt: ["Computer Science", "Engineering"] } }
b) { major: { $in: ["Computer Science", "Engineering"] } }
c) { major: { $lt: ["Computer Science", "Engineering"] } }
d) { major: "Computer Science" }
Answer: b) { major: { $in: ["Computer Science", "Engineering"] } }
Explanation: The $in operator is used to find documents where the major is either "Computer Science" or "Engineering".
40. What is the correct operator for finding students whose age is not 18?
a) { age: { $neq: 18 } }
b) { age: { $ne: 18 } }
c) { age: { $eq: 18 } }
d) { age: { $gt: 18 } }
Answer: b) { age: { $ne: 18 } }
Explanation: The $ne operator is used to find documents where the value is not equal to 18.
41. What does the $regex operator allow you to do?
a) Check for equality
b) Perform mathematical comparisons
c) Perform pattern matching on string fields
d) Check if a field exists
Answer: c) Perform pattern matching on string fields
Explanation: The $regex operator allows you to perform regular expression pattern matching on string fields.
42. Which operator is used to query for documents where the value of a field is less than or equal to a specified value?
a) $gt
b) $lt
c) $gte
d) $lte
Answer: d) $lte
Explanation: The $lte operator is used to find documents where the field's value is less than or equal to the specified value.
43. Which of the following queries will find students aged greater than 18 and less than 25?
a) { age: { $gt: 18, $lt: 25 } }
b) { age: { $gt: 18, $lt: 30 } }
c) { age: { $eq: 25 } }
d) { age: { $in: [20, 22, 25] } }
Answer: a) { age: { $gt: 18, $lt: 25 } }
Explanation: This query uses both $gt (greater than) and $lt (less than) to select students aged greater than 18 and less than 25.
44. Which operator checks if a field value exists and is not null?
a) $eq
b) $exists
c) $lt
d) $ne
Answer: b) $exists
Explanation: The $exists operator checks if a field is present in the document.
45. Which of the following queries will return students where the name does not contain "Smith"?
a) { name: { $regex: /^Smith/ } }
b) { name: { $regex: /Smith/ } }
c) { name: { $ne: "Smith" } }
d) { name: { $not: "Smith" } }
Answer: c) { name: { $ne: "Smith" } }
Explanation: The $ne operator is used to exclude documents with a field value equal to "Smith."
46. Which query returns all students whose major is not "Physics"?
a) { major: { $ne: "Physics" } }
b) { major: "Physics" }
c) { major: { $in: ["Physics"] } }
d) { major: { $not: "Physics" } }
Answer: a) { major: { $ne: "Physics" } }
Explanation: The $ne operator is used to find documents where the major is not equal to "Physics."
47. Which query will find all documents where the age is greater than 30?
a) { age: { $gt: 30 } }
b) { age: { $lt: 30 } }
c) { age: { $gte: 30 } }
d) { age: { $lte: 30 } }
Answer: a) { age: { $gt: 30 } }
Explanation: The $gt operator is used to find documents where the value of the age field is greater than 30.
48. Which query will return students whose name is exactly "John"?
a) { name: "John" }
b) { name: { $eq: "John" } }
c) { name: { $regex: "John" } }
d) { name: { $gt: "John" } }
Answer: a) { name: "John" }
Explanation: To match an exact string, you can directly query with { name: "John" }.
49. Which operator would you use to find students with a name that contains the substring "Math"?
a) $regex
b) $exists
c) $lt
d) $gt
Answer: a) $regex
Explanation: The $regex operator allows you to search for documents with a field matching a pattern, in this case, the substring "Math."
50. Which of the following operators is used to find documents where a field is less than or equal to a specified value?
a) $gt
b) $lt
c) $gte
d) $lte
Answer: d) $lte
Explanation: The $lte operator is used to find documents where the field value is less than or equal to the specified value.
