MCQs Of Day 6: MongoDB CRUD Operations – Part 2 (Find)

Rashmi Mishra
0

 MCQs Of Day 6: MongoDB CRUD Operations – Part 2 (Find)


1. Which method is used to retrieve documents in MongoDB?

A) insert()
B) find()
C) update()
D) delete()

Answer: B) find()
Explanation: The find() method is used to retrieve documents from a MongoDB collection.


2. How do you retrieve all documents from the students collection?

A) db.students.retrieve()
B) db.students.find()
C) db.find.students()
D) db.students.fetch()

Answer: B) db.students.find()
Explanation: find() is the correct method to retrieve all documents from a collection.


3. What is the default behavior of the find() method if no arguments are passed?

A) It returns an empty result.
B) It retrieves only one document.
C) It retrieves all documents in the collection.
D) It throws an error.

Answer: C) It retrieves all documents in the collection.
Explanation: If no query argument is passed, find() retrieves all documents in the collection.


4. What does the following query do? db.students.find({ age: 22 })

A) Returns students with age equal to 22.
B) Returns students with age greater than 22.
C) Returns students with age less than 22.
D) Returns students with age greater than or equal to 22.

Answer: A) Returns students with age equal to 22.
Explanation: The query filters the documents where the age field is exactly 22.


5. What operator is used to find documents where a field is greater than or equal to a value?

A) $lt
B) $gt
C) $gte
D) $ne

Answer: C) $gte
Explanation: The $gte operator is used to match documents where the field value is greater than or equal to the specified value.


6. Which of the following is a valid comparison operator in MongoDB?

A) $equal
B) $greaterThan
C) $gte
D) $biggerThan

Answer: C) $gte
Explanation: $gte is a valid comparison operator that stands for "greater than or equal to."


7. Which query would return students with age 20, 22, or 24?

A) db.students.find({ age: { $eq: [20, 22, 24] } })
B) db.students.find({ age: { $in: [20, 22, 24] } })
C) db.students.find({ age: { $gt: [20, 22, 24] } })
D) db.students.find({ age: { $in: [20, 22, 24] } })

Answer: B) db.students.find({ age: { $in: [20, 22, 24] } })
Explanation: The $in operator is used to find documents where the field value is within an array of values.


8. How do you find documents where a student's age is greater than 18 and grade is greater than 75?

A) db.students.find({ age: { $gt: 18 }, grade: { $gt: 75 } })
B) db.students.find({ age: { $gt: 18 } }, { grade: { $gt: 75 } })
C) db.students.find({ age: { $lt: 18 }, grade: { $gt: 75 } })
D) db.students.find({ age: 18 }, { grade: { $gt: 75 } })

Answer: A) db.students.find({ age: { $gt: 18 }, grade: { $gt: 75 } })
Explanation: The $gt operator is used to filter documents where age is greater than 18 and grade is greater than 75.


9. What will the following query return? db.students.find({ $or: [{ age: 21 }, { grade: { $gte: 85 } }] })

A) Students aged 21 or with grades greater than or equal to 85.
B) Students aged 21 and with grades greater than or equal to 85.
C) Students aged less than 21 or with grades greater than or equal to 85.
D) Students aged less than 21 and with grades less than 85.

Answer: A) Students aged 21 or with grades greater than or equal to 85.
Explanation: The $or operator allows you to retrieve students who either have age equal to 21 or grade greater than or equal to 85.


10. How do you query a nested field in MongoDB?

A) By using the dot notation.
B) By using the [] brackets.
C) By using the . operator.
D) By using the > operator.

Answer: A) By using the dot notation.
Explanation: MongoDB allows you to query nested fields by using dot notation, e.g., address.city.


11. Which of the following queries retrieves students who live in the city "New York"?

A) db.students.find({ address: { city: "New York" } })
B) db.students.find({ "address.city": "New York" })
C) db.students.find({ address: "New York" })
D) db.students.find({ city: "New York" })

Answer: B) db.students.find({ "address.city": "New York" })
Explanation: Dot notation ("address.city") is used to query a nested field.


12. What is the purpose of the limit() method in MongoDB queries?

A) To limit the number of documents returned by the query.
B) To sort the documents.
C) To modify the documents.
D) To remove duplicate documents from the result.

Answer: A) To limit the number of documents returned by the query.
Explanation: The limit() method restricts the number of documents returned by a query.


13. How do you limit the query results to 3 documents?

A) db.students.find().limit(3)
B) db.students.find().max(3)
C) db.students.find().top(3)
D) db.students.find().stop(3)

Answer: A) db.students.find().limit(3)
Explanation: The limit() method is used to specify the maximum number of documents to return.


14. Which of the following queries sorts students by age in ascending order?

A) db.students.find().sort({ age: 1 })
B) db.students.find().sort({ age: -1 })
C) db.students.find().sort({ age: "ascending" })
D) db.students.find().sort({ age: "desc" })

Answer: A) db.students.find().sort({ age: 1 })
Explanation: The 1 value in sort() indicates ascending order.


15. Which query returns students ordered by age in descending order?

A) db.students.find().sort({ age: 1 })
B) db.students.find().sort({ age: -1 })
C) db.students.find().order({ age: -1 })
D) db.students.find().orderBy({ age: 1 })

Answer: B) db.students.find().sort({ age: -1 })
Explanation: The -1 value in sort() indicates descending order.


16. Which of the following is the correct way to exclude the _id field from query results?

A) db.students.find({}, { _id: 0 })
B) db.students.find({ _id: null })
C) db.students.find({ _id: false })
D) db.students.find({ _id: "exclude" })

Answer: A) db.students.find({}, { _id: 0 })
Explanation: Setting _id to 0 in the projection excludes the _id field.


17. What does the projection argument in the find() method specify?

A) The collection name to search in.
B) The maximum number of documents to return.
C) The fields to include or exclude in the query result.
D) The sorting order for the results.

Answer: C) The fields to include or exclude in the query result.
Explanation: The projection specifies which fields to return or exclude.


18. How do you query students with a grade greater than or equal to 85?

A) db.students.find({ grade: { $lt: 85 } })
B) db.students.find({ grade: { $gte: 85 } })
C) db.students.find({ grade: { $ne: 85 } })
D) db.students.find({ grade: { $gt: 85 } })

Answer: B) db.students.find({ grade: { $gte: 85 } })
Explanation: The $gte operator filters documents where grade is greater than or equal to 85.


19. What will this query return? db.students.find({ age: { $ne: 20 } })

A) All students whose age is not equal to 20.
B) All students whose age is equal to 20.
C) All students whose age is greater than 20.
D) All students whose age is less than 20.

Answer: A) All students whose age is not equal to 20.
Explanation: The $ne operator means "not equal to."


20. Which operator is used to match documents where a field value is different from a given value?

A) $ne
B) $eq
C) $gte
D) $lte

Answer: A) $ne
Explanation: The $ne operator is used to find documents where the field value is not equal to the specified value.


(Continued in the next response.)

4o mini

You said:

yes

ChatGPT said:

ChatGPT

Here are the remaining Multiple Choice Questions (MCQs):


21. How do you query documents where age is either 18 or 21?

A) db.students.find({ age: { $eq: [18, 21] } })
B) db.students.find({ age: { $in: [18, 21] } })
C) db.students.find({ age: { $gte: 18, $lte: 21 } })
D) db.students.find({ age: { $neq: 18, 21 } })

Answer: B) db.students.find({ age: { $in: [18, 21] } })
Explanation: The $in operator is used to find documents where the age field matches any of the specified values (18 or 21).


22. Which operator is used to match documents where a field is less than a specified value?

A) $lt
B) $gt
C) $gte
D) $lte

Answer: A) $lt
Explanation: The $lt operator is used to find documents where the field value is less than the specified value.


23. Which query would retrieve students whose name starts with the letter "A"?

A) db.students.find({ name: { $regex: "^A" } })
B) db.students.find({ name: { $startsWith: "A" } })
C) db.students.find({ name: { $match: "A" } })
D) db.students.find({ name: { $like: "A%" } })

Answer: A) db.students.find({ name: { $regex: "^A" } })
Explanation: The $regex operator is used to match text patterns, and ^A specifies that the string should start with "A."


24. How would you find students whose name ends with the letter "n"?

A) db.students.find({ name: { $regex: "n$" } })
B) db.students.find({ name: { $endsWith: "n" } })
C) db.students.find({ name: { $like: "%n" } })
D) db.students.find({ name: { $match: "n$" } })

Answer: A) db.students.find({ name: { $regex: "n$" } })
Explanation: The $regex operator with n$ matches names that end with the letter "n."


25. What does the skip() method in MongoDB do?

A) Skips documents that match the query.
B) Skips the first N documents in the result set.
C) Skips documents from the specified collection.
D) Skips documents that exceed a certain size.

Answer: B) Skips the first N documents in the result set.
Explanation: The skip() method is used to skip the first N documents of a query result.


26. How do you combine limit() and skip() in a query?

A) db.students.find().skip(5).limit(3)
B) db.students.find().limit(3).skip(5)
C) db.students.find().skip(3).limit(5)
D) db.students.find().limit(5).skip(3)

Answer: A) db.students.find().skip(5).limit(3)
Explanation: You can combine skip() and limit() to paginate the results, skipping the first 5 documents and then limiting the result to 3 documents.


27. What is the purpose of the count() method in MongoDB?

A) To count the number of documents in a collection.
B) To count the number of fields in a document.
C) To count the number of queries executed.
D) To count the number of indexes in a collection.

Answer: A) To count the number of documents in a collection.
Explanation: The count() method returns the number of documents that match a given query.


28. How do you find the total number of students who have age greater than 18?

A) db.students.find({ age: { $gt: 18 } }).count()
B) db.students.find({ age: { $gt: 18 } }).total()
C) db.students.count({ age: { $gt: 18 } })
D) db.students.countDocuments({ age: { $gt: 18 } })

Answer: A) db.students.find({ age: { $gt: 18 } }).count()
Explanation: The count() method returns the number of documents that match the query.


29. How do you perform an exact match query in MongoDB?

A) Using the $eq operator.
B) Using the $exact operator.
C) Using the $match operator.
D) Using the $regex operator.

Answer: A) Using the $eq operator.
Explanation: The $eq operator is used to perform an exact match on a field's value.


30. What will this query return? db.students.find({ name: { $exists: true } })

A) Students whose name field exists in the document.
B) Students whose name field is missing.
C) Students whose name field is empty.
D) Students whose name field equals "true."

Answer: A) Students whose name field exists in the document.
Explanation: The $exists operator checks if a field is present in the document.


31. How do you query documents where age is not specified (i.e., missing)?

A) db.students.find({ age: { $exists: false } })
B) db.students.find({ age: { $missing: true } })
C) db.students.find({ age: null })
D) db.students.find({ age: { $empty: true } })

Answer: A) db.students.find({ age: { $exists: false } })
Explanation: The $exists: false query matches documents where the age field is not present.


32. How would you search for students whose name contains "John"?

A) db.students.find({ name: { $regex: "John" } })
B) db.students.find({ name: { $contains: "John" } })
C) db.students.find({ name: { $like: "John%" } })
D) db.students.find({ name: { $match: "John" } })

Answer: A) db.students.find({ name: { $regex: "John" } })
Explanation: The $regex operator can be used to find substrings within a string field.


33. Which query finds students whose age is between 18 and 25?

A) db.students.find({ age: { $gte: 18, $lte: 25 } })
B) db.students.find({ age: { $in: [18, 25] } })
C) db.students.find({ age: { $lt: 18, $gt: 25 } })
D) db.students.find({ age: { $gt: 18, $lt: 25 } })

Answer: A) db.students.find({ age: { $gte: 18, $lte: 25 } })
Explanation: The $gte and $lte operators are used to match a range of values.


34. What does the $ne operator do in a query?

A) It matches documents where the value of a field is equal to the specified value.
B) It matches documents where the value of a field is not equal to the specified value.
C) It matches documents where the value of a field is greater than the specified value.
D) It matches documents where the value of a field is less than the specified value.

Answer: B) It matches documents where the value of a field is not equal to the specified value.
Explanation: The $ne operator is used for "not equal" comparisons.


35. How do you perform a case-insensitive search for "john"?

A) db.students.find({ name: { $regex: /john/i } })
B) db.students.find({ name: { $match: "john" } })
C) db.students.find({ name: { $regex: "john" } })
D) db.students.find({ name: { $caseInsensitive: "john" } })

Answer: A) db.students.find({ name: { $regex: /john/i } })
Explanation: The /i flag in regular expressions makes the search case-insensitive.


36. Which operator is used for a "not equal" comparison in MongoDB?

A) $eq
B) $ne
C) $gt
D) $lt

Answer: B) $ne
Explanation: The $ne operator is used to find documents where the field value is not equal to a specified value.


37. What will this query return? db.students.find({ grade: { $in: [85, 90, 95] } })

A) Students with a grade of 85, 90, or 95.
B) Students with grades greater than or equal to 85.
C) Students with grades less than 85.
D) Students whose grade is not in [85, 90, 95].

Answer: A) Students with a grade of 85, 90, or 95.
Explanation: The $in operator finds documents where the grade field matches any of the specified values.


(Continued in the next response...)

4o mini

You said:

yes

ChatGPT said:

ChatGPT

Here are more Multiple Choice Questions (MCQs):


38. What does the $gt operator do in a query?

A) Matches documents where the field value is greater than the specified value.
B) Matches documents where the field value is greater than or equal to the specified value.
C) Matches documents where the field value is less than the specified value.
D) Matches documents where the field value is equal to the specified value.

Answer: A) Matches documents where the field value is greater than the specified value.
Explanation: The $gt operator finds documents where the value of the field is greater than the specified value.


39. Which query retrieves all students whose age is either 20 or 25?

A) db.students.find({ age: { $eq: [20, 25] } })
B) db.students.find({ age: { $in: [20, 25] } })
C) db.students.find({ age: { $gt: 20, $lt: 25 } })
D) db.students.find({ age: { $gte: 20, $lte: 25 } })

Answer: B) db.students.find({ age: { $in: [20, 25] } })
Explanation: The $in operator is used to match documents where the field value is one of the specified values, in this case, 20 or 25.


40. How can you retrieve all students whose names are not null?

A) db.students.find({ name: { $ne: null } })
B) db.students.find({ name: { $exists: true } })
C) db.students.find({ name: { $null: false } })
D) db.students.find({ name: { $notNull: true } })

Answer: A) db.students.find({ name: { $ne: null } })
Explanation: The $ne operator with null excludes documents where the name field is null.


41. Which query finds students whose grade is not in [90, 95, 100]?

A) db.students.find({ grade: { $not: [90, 95, 100] } })
B) db.students.find({ grade: { $nin: [90, 95, 100] } })
C) db.students.find({ grade: { $neq: [90, 95, 100] } })
D) db.students.find({ grade: { $notIn: [90, 95, 100] } })

Answer: B) db.students.find({ grade: { $nin: [90, 95, 100] } })
Explanation: The $nin operator matches documents where the field value is not in the specified array.


42. What does the $exists operator do in a query?

A) Finds documents where the field is equal to true.
B) Finds documents where the field is missing or null.
C) Finds documents where the field exists or is present.
D) Finds documents where the field is empty.

Answer: C) Finds documents where the field exists or is present.
Explanation: The $exists operator checks if a field is present in a document, regardless of its value.


43. How would you retrieve students who do not have an age field?

A) db.students.find({ age: { $exists: false } })
B) db.students.find({ age: { $notExist: true } })
C) db.students.find({ age: { $isNull: true } })
D) db.students.find({ age: null })

Answer: A) db.students.find({ age: { $exists: false } })
Explanation: The $exists: false operator finds documents where the age field does not exist.


44. How would you find students who have a grade greater than 85 and less than 95?

A) db.students.find({ grade: { $gt: 85, $lt: 95 } })
B) db.students.find({ grade: { $gte: 85, $lte: 95 } })
C) db.students.find({ grade: { $in: [85, 95] } })
D) db.students.find({ grade: { $eq: [85, 95] } })

Answer: A) db.students.find({ grade: { $gt: 85, $lt: 95 } })
Explanation: The $gt and $lt operators find documents where the field value is greater than 85 and less than 95.


45. Which query retrieves students whose grade is exactly 85?

A) db.students.find({ grade: { $eq: 85 } })
B) db.students.find({ grade: 85 })
C) db.students.find({ grade: { $gt: 85 } })
D) db.students.find({ grade: { $gte: 85 } })

Answer: B) db.students.find({ grade: 85 })
Explanation: You can query a field directly by specifying the value you are looking for (exact match).


46. What is the result of this query: db.students.find({ age: { $lt: 30 } }).skip(5).limit(10)?

A) Retrieves the first 5 students whose age is less than 30.
B) Skips the first 5 students and then retrieves the next 10 students whose age is less than 30.
C) Skips the first 10 students and then retrieves the next 5 students whose age is less than 30.
D) Skips the first 10 students and then retrieves the next 5 students.

Answer: B) Skips the first 5 students and then retrieves the next 10 students whose age is less than 30.
Explanation: The skip() method skips the first N documents, and limit() restricts the number of results returned.


47. How do you perform a query where the field value is greater than or equal to 50?

A) db.students.find({ grade: { $gte: 50 } })
B) db.students.find({ grade: { $gt: 50 } })
C) db.students.find({ grade: { $lte: 50 } })
D) db.students.find({ grade: { $ne: 50 } })

Answer: A) db.students.find({ grade: { $gte: 50 } })
Explanation: The $gte operator matches documents where the field value is greater than or equal to the specified value.


48. How do you use find() to search for documents with an exact string match?

A) db.students.find({ name: "John" })
B) db.students.find({ name: { $exact: "John" } })
C) db.students.find({ name: { $eq: "John" } })
D) db.students.find({ name: { $regex: "John" } })

Answer: A) db.students.find({ name: "John" })
Explanation: To find an exact match for a string, you can directly specify the field and value.


49. What does the $lt operator do in MongoDB queries?

A) Matches documents where the field value is less than or equal to the specified value.
B) Matches documents where the field value is greater than the specified value.
C) Matches documents where the field value is less than the specified value.
D) Matches documents where the field value is equal to the specified value.

Answer: C) Matches documents where the field value is less than the specified value.
Explanation: The $lt operator finds documents where the field value is less than the specified value.


50. How do you find all students whose names are exactly "Alice"?

A) db.students.find({ name: "Alice" })
B) db.students.find({ name: { $eq: "Alice" } })
C) db.students.find({ name: { $exact: "Alice" } })
D) db.students.find({ name: { $match: "Alice" } })

Answer: A) db.students.find({ name: "Alice" })
Explanation: You can directly query for an exact match on the field by specifying the value you want.


These are the MCQs based on MongoDB CRUD Operations (Part 2 - Find). Let me know if you need further help!

4o mini

You said:

rest 50

ChatGPT said:

ChatGPT

Here are the remaining 50 Multiple Choice Questions (MCQs) for MongoDB CRUD Operations – Part 2 (Find):


51. What will the query db.students.find({ age: { $gt: 18, $lt: 30 } }) return?

A) Students aged exactly 18 and 30.
B) Students aged greater than 18 but less than 30.
C) Students aged greater than or equal to 18.
D) Students aged less than or equal to 30.

Answer: B) Students aged greater than 18 but less than 30.
Explanation: The query finds documents where the age field is greater than 18 and less than 30.


52. Which operator would you use to find documents where the age is neither 18 nor 21?

A) $not
B) $eq
C) $in
D) $nin

Answer: D) $nin
Explanation: The $nin operator is used to find documents where the field value is not in the specified list of values.


53. How can you limit the number of results returned by a query to 5 documents?

A) db.students.find().limit(5)
B) db.students.find().count(5)
C) db.students.find().skip(5)
D) db.students.find().top(5)

Answer: A) db.students.find().limit(5)
Explanation: The limit() function is used to limit the number of documents returned by a query.


54. What does the $ne operator do?

A) Finds documents where the field is equal to the specified value.
B) Finds documents where the field is greater than the specified value.
C) Finds documents where the field is not equal to the specified value.
D) Finds documents where the field value is null.

Answer: C) Finds documents where the field is not equal to the specified value.
Explanation: The $ne operator returns documents where the specified field value does not match the provided value.


55. What is the purpose of the skip() method in MongoDB?

A) It removes duplicate documents from the result.
B) It skips the first N documents in the result set.
C) It returns only unique values for a specific field.
D) It returns documents after applying a filter.

Answer: B) It skips the first N documents in the result set.
Explanation: The skip() method is used to skip a specified number of documents in the query results.


56. Which operator allows you to check if a field contains a specific substring?

A) $in
B) $contains
C) $regex
D) $like

Answer: C) $regex
Explanation: The $regex operator allows you to use regular expressions to search for a substring within a field value.


57. What does the query db.students.find({ grade: { $gte: 90 } }).sort({ grade: -1 }) do?

A) Finds all students with a grade greater than or equal to 90 and sorts them in ascending order by grade.
B) Finds all students with a grade greater than or equal to 90 and sorts them in descending order by grade.
C) Finds all students with a grade less than 90 and sorts them in ascending order by grade.
D) Finds all students with a grade greater than or equal to 90 and sorts them alphabetically by name.

Answer: B) Finds all students with a grade greater than or equal to 90 and sorts them in descending order by grade.
Explanation: The sort({ grade: -1 }) sorts the results in descending order by the grade field.


58. Which query finds all students whose names contain the substring "John"?

A) db.students.find({ name: { $regex: "John" } })
B) db.students.find({ name: { $contains: "John" } })
C) db.students.find({ name: "John" })
D) db.students.find({ name: { $like: "John" } })

Answer: A) db.students.find({ name: { $regex: "John" } })
Explanation: The $regex operator allows you to match documents where the field contains the specified substring.


59. How do you use find() to query for students who have not completed a course?

A) db.students.find({ courseCompleted: { $exists: false } })
B) db.students.find({ courseCompleted: null })
C) db.students.find({ courseCompleted: { $ne: true } })
D) db.students.find({ courseCompleted: { $not: true } })

Answer: A) db.students.find({ courseCompleted: { $exists: false } })
Explanation: The $exists: false operator finds documents where the courseCompleted field is not present.


60. Which of the following operators is used to match values greater than or equal to a specific value?

A) $gt
B) $gte
C) $lt
D) $neq

Answer: B) $gte
Explanation: The $gte operator is used to match documents where the field value is greater than or equal to the specified value.


61. What will the query db.students.find({ age: { $lt: 30, $gt: 18 } }) return?

A) Students with an age greater than 30.
B) Students with an age less than 30 and greater than 18.
C) Students with an age greater than or equal to 18.
D) Students with an age greater than 30 or less than 18.

Answer: B) Students with an age less than 30 and greater than 18.
Explanation: The query uses the $lt and $gt operators to find students whose age is between 18 and 30.


62. Which operator would you use to find documents where a specific field does not exist?

A) $exists
B) $notExist
C) $missing
D) $null

Answer: A) $exists
Explanation: The $exists operator is used to check whether a field exists or not in the document.


63. How would you find students whose names are exactly "Alice" or "Bob"?

A) db.students.find({ name: { $in: ["Alice", "Bob"] } })
B) db.students.find({ name: { $or: ["Alice", "Bob"] } })
C) db.students.find({ name: { $eq: "Alice" }, name: { $eq: "Bob" } })
D) db.students.find({ name: { $matches: "Alice|Bob" } })

Answer: A) db.students.find({ name: { $in: ["Alice", "Bob"] } })
Explanation: The $in operator matches documents where the field value is one of the specified values, in this case, "Alice" or "Bob."


64. Which operator should be used to query documents with an exact match for a numeric value?

A) $exact
B) $eq
C) $match
D) $num

Answer: B) $eq
Explanation: The $eq operator is used for exact matching, including for numeric values.


65. What does the query db.students.find({ age: { $lt: 25 } }).count() do?

A) Returns the count of students aged less than 25.
B) Returns all students aged less than 25.
C) Returns the total number of documents in the collection.
D) Returns the sum of ages for students less than 25.

Answer: A) Returns the count of students aged less than 25.
Explanation: The count() method returns the number of documents that match the specified query.


66. Which query returns students whose names start with "A"?

A) db.students.find({ name: { $regex: "^A" } })
B) db.students.find({ name: { $regex: "A$" } })
C) db.students.find({ name: { $in: "A" } })
D) db.students.find({ name: { $like: "^A" } })

Answer: A) db.students.find({ name: { $regex: "^A" } })
Explanation: The ^ in the regular expression matches documents where the name field starts with "A".


67. How do you find students who scored greater than 80 but less than 90 in their exams?

A) db.students.find({ grade: { $gt: 80, $lt: 90 } })
B) db.students.find({ grade: { $gte: 80, $lte: 90 } })
C) db.students.find({ grade: { $eq: 80, $eq: 90 } })
D) db.students.find({ grade: { $in: [80, 90] } })

Answer: A) db.students.find({ grade: { $gt: 80, $lt: 90 } })
Explanation: The $gt and $lt operators are used to find documents where the grade field is greater than 80 and less than 90.


68. What does the db.students.find().sort({ age: 1 }) query do?

A) Sorts the results by age in descending order.
B) Sorts the results by age in ascending order.
C) Finds students aged 1 year.
D) Sorts the results by age in random order.

Answer: B) Sorts the results by age in ascending order.
Explanation: The 1 in the sort() function sorts the age field in ascending order.


69. How would you find students who do not have a grade specified?

A) db.students.find({ grade: { $exists: false } })
B) db.students.find({ grade: { $eq: null } })
C) db.students.find({ grade: { $not: "" } })
D) db.students.find({ grade: { $in: [] } })

Answer: A) db.students.find({ grade: { $exists: false } })
Explanation: The $exists: false operator finds documents where the grade field is missing.


70. How would you find students with a status field equal to "active"?

A) db.students.find({ status: "active" })
B) db.students.find({ status: { $eq: "active" } })
C) db.students.find({ status: { $gt: "active" } })
D) db.students.find({ status: { $lt: "active" } })

Answer: A) db.students.find({ status: "active" })
Explanation: The query finds documents where the status field is exactly "active".


71. Which query would find students whose grades are less than 70 and greater than 50?

A) db.students.find({ grade: { $gt: 70, $lt: 50 } })
B) db.students.find({ grade: { $gt: 50, $lt: 70 } })
C) db.students.find({ grade: { $lt: 50, $gt: 70 } })
D) db.students.find({ grade: { $eq: 50 } })

Answer: B) db.students.find({ grade: { $gt: 50, $lt: 70 } })
Explanation: This query uses the $gt and $lt operators to find grades between 50 and 70.


72. Which of the following would return the highest grade from the students collection?

A) db.students.find().sort({ grade: -1 }).limit(1)
B) db.students.find().sort({ grade: 1 }).limit(1)
C) db.students.find({ grade: { $gt: 100 } })
D) db.students.find().max({ grade: 1 })

Answer: A) db.students.find().sort({ grade: -1 }).limit(1)
Explanation: Sorting by grade in descending order and limiting the result to 1 returns the highest grade.


73. What is the purpose of using the distinct() method?

A) It returns the total number of documents in the collection.
B) It returns unique values for a specified field.
C) It sorts the documents in ascending order.
D) It removes duplicate documents from the collection.

Answer: B) It returns unique values for a specified field.
Explanation: The distinct() method returns an array of distinct values for the specified field in the collection.


74. What does the query db.students.find({ age: { $lte: 18 } }) do?

A) Finds students with an age greater than or equal to 18.
B) Finds students with an age less than or equal to 18.
C) Finds students who are older than 18.
D) Finds students whose age is exactly 18.

Answer: B) Finds students with an age less than or equal to 18.
Explanation: The $lte operator finds documents where the age is less than or equal to the specified value (18).


75. Which of the following query would find all students who have not scored a grade yet (i.e., grade is null)?

A) db.students.find({ grade: null })
B) db.students.find({ grade: { $exists: true } })
C) db.students.find({ grade: { $eq: "" } })
D) db.students.find({ grade: { $eq: null } })

Answer: A) db.students.find({ grade: null })
Explanation: This query matches documents where the grade field is explicitly set to null.


76. Which operator is used to find documents where a field is an array and contains a specific value?

A) $all
B) $elemMatch
C) $in
D) $contains

Answer: B) $elemMatch
Explanation: The $elemMatch operator is used to match documents where a field is an array and contains a specific value.


77. Which operator would you use to query for documents where a field contains any one of several specified values?

A) $all
B) $in
C) $eq
D) $exists

Answer: B) $in
Explanation: The $in operator allows querying for documents where a field contains any one of several specified values.


78. Which of the following queries will find students with exactly "A+" as their grade?

A) db.students.find({ grade: "A+" })
B) db.students.find({ grade: { $eq: "A+" } })
C) db.students.find({ grade: { $matches: "A+" } })
D) db.students.find({ grade: { $regex: "^A+" } })

Answer: A) db.students.find({ grade: "A+" })
Explanation: The query finds documents where the grade field is exactly "A+".


79. How do you find students who have either completed or not completed their courses?

A) db.students.find({ courseCompleted: { $exists: true } })
B) db.students.find({ courseCompleted: { $in: ["true", "false"] } })
C) db.students.find({ courseCompleted: { $eq: true } })
D) db.students.find({ courseCompleted: { $ne: null } })

Answer: B) db.students.find({ courseCompleted: { $in: ["true", "false"] } })
Explanation: The $in operator can match either true or false for the courseCompleted field.


80. What is the function of explain() in MongoDB?

A) Provides detailed statistics about the query execution.
B) Provides a summary of the collection.
C) Describes how a query is structured.
D) Provides debugging information.

Answer: A) Provides detailed statistics about the query execution.
Explanation: The explain() function gives details about how MongoDB executes a query, which can be useful for optimization.

4o mini

You said:

rest

ChatGPT said:

ChatGPT

Here are the remaining questions:


81. How would you find students who are in the class with ID 101 and have an age greater than or equal to 18?

A) db.students.find({ class_id: 101, age: { $gte: 18 } })
B) db.students.find({ class_id: 101, age: { $lt: 18 } })
C) db.students.find({ class_id: 101, age: { $eq: 18 } })
D) db.students.find({ class_id: 101, age: 18 })

Answer: A) db.students.find({ class_id: 101, age: { $gte: 18 } })
Explanation: This query finds students in class 101 with age greater than or equal to 18.


82. How do you update a student’s grade in the students collection?

A) db.students.update({ name: "John" }, { grade: "A+" })
B) db.students.updateOne({ name: "John" }, { $set: { grade: "A+" } })
C) db.students.find({ name: "John" }).set({ grade: "A+" })
D) db.students.updateMany({ name: "John" }, { grade: "A+" })

Answer: B) db.students.updateOne({ name: "John" }, { $set: { grade: "A+" } })
Explanation: The updateOne() method updates the first document that matches the condition. The $set operator updates the value of the specified field.


83. Which of the following methods allows you to add a new field to an existing document?

A) db.students.addField({ field: "age" })
B) db.students.insertOne({ field: "age" })
C) db.students.updateOne({ name: "John" }, { $set: { age: 20 } })
D) db.students.updateMany({ name: "John" }, { addField: "age" })

Answer: C) db.students.updateOne({ name: "John" }, { $set: { age: 20 } })
Explanation: The $set operator is used to add a new field or update an existing one.


84. Which query will find students whose grades are either 'A' or 'B'?

A) db.students.find({ grade: { $in: ["A", "B"] } })
B) db.students.find({ grade: "A", "B" })
C) db.students.find({ grade: { $eq: ["A", "B"] } })
D) db.students.find({ grade: { $not: ["A", "B"] } })

Answer: A) db.students.find({ grade: { $in: ["A", "B"] } })
Explanation: The $in operator checks if the value of the grade field is either "A" or "B".


85. Which operator is used to match values that are greater than or equal to a specified value?

A) $lte
B) $gt
C) $gte
D) $lt

Answer: C) $gte
Explanation: The $gte operator matches values greater than or equal to the specified value.


86. How do you delete a student with the name "John" from the collection?

A) db.students.remove({ name: "John" })
B) db.students.deleteOne({ name: "John" })
C) db.students.removeOne({ name: "John" })
D) db.students.deleteMany({ name: "John" })

Answer: B) db.students.deleteOne({ name: "John" })
Explanation: The deleteOne() method deletes the first document that matches the condition.


87. How would you aggregate the total number of students in each class?

A) db.students.aggregate([{ $group: { _id: "$class_id", total: { $sum: 1 } } }])
B) db.students.aggregate([{ $sum: { _id: "$class_id" } }])
C) db.students.aggregate([{ $count: { _id: "$class_id" } }])
D) db.students.aggregate([{ $group: { total: { $sum: "$class_id" } } }])

Answer: A) db.students.aggregate([{ $group: { _id: "$class_id", total: { $sum: 1 } } }])
Explanation: The $group stage groups the students by class_id, and $sum: 1 counts the total number of students in each class.


88. What is the purpose of using the limit() method?

A) Limits the number of fields returned.
B) Limits the number of documents returned.
C) Limits the size of the database.
D) Limits the query execution time.

Answer: B) Limits the number of documents returned.
Explanation: The limit() method restricts the number of documents returned in the result.


89. How can you find the average grade of all students?

A) db.students.aggregate([{ $avg: { grade: 1 } }])
B) db.students.aggregate([{ $group: { _id: null, avgGrade: { $avg: "$grade" } } }])
C) db.students.find({ grade: { $avg: true } })
D) db.students.aggregate([{ $group: { avgGrade: "$grade" } }])

Answer: B) db.students.aggregate([{ $group: { _id: null, avgGrade: { $avg: "$grade" } } }])
Explanation: The $avg operator calculates the average value of the grade field.


90. How would you find students who have the grade 'A' and an age less than 20?

A) db.students.find({ grade: "A", age: { $lt: 20 } })
B) db.students.find({ grade: "A", age: { $gt: 20 } })
C) db.students.find({ grade: { $eq: "A" }, age: { $lt: 20 } })
D) db.students.find({ grade: "A", age: { $eq: 20 } })

Answer: A) db.students.find({ grade: "A", age: { $lt: 20 } })
Explanation: This query finds students who have grade "A" and are under 20 years old.


91. Which operator would you use to query for documents where a field value is not equal to a specified value?

A) $not
B) $ne
C) $notEqual
D) $neq

Answer: B) $ne
Explanation: The $ne operator is used to find documents where the field value is not equal to the specified value.


92. Which method would you use to update multiple documents that match a specific condition?

A) db.students.updateOne()
B) db.students.updateMany()
C) db.students.update()
D) db.students.updateAll()

Answer: B) db.students.updateMany()
Explanation: The updateMany() method updates all documents that match the condition.


93. How would you retrieve only the name and age fields of students?

A) db.students.find({}, { name: 1, age: 1 })
B) db.students.find({ name: 1, age: 1 })
C) db.students.find({ name: "John", age: 1 })
D) db.students.find({ name: "John", age: { $eq: 1 } })

Answer: A) db.students.find({}, { name: 1, age: 1 })
Explanation: The second argument of find() specifies the fields to include. 1 includes those fields in the result.


94. Which query would you use to remove all students whose grade is null?

A) db.students.deleteMany({ grade: null })
B) db.students.remove({ grade: null })
C) db.students.delete({ grade: null })
D) db.students.removeMany({ grade: null })

Answer: A) db.students.deleteMany({ grade: null })
Explanation: The deleteMany() method removes all documents that match the condition.


95. How do you find documents with a specific field value using the $regex operator?

A) db.students.find({ grade: { $regex: "A" } })
B) db.students.find({ grade: { $matches: "A" } })
C) db.students.find({ grade: { $contains: "A" } })
D) db.students.find({ grade: { $like: "A" } })

Answer: A) db.students.find({ grade: { $regex: "A" } })
Explanation: The $regex operator is used to find documents where a field value matches the specified pattern.


96. Which operator is used to find documents where the field value is an array and contains a specific value?

A) $all
B) $in
C) $elemMatch
D) $array

Answer: C) $elemMatch
Explanation: The $elemMatch operator is used to match documents where an array field contains at least one element that satisfies the condition.


97. Which of the following queries will count the total number of students in a collection?

A) db.students.count()
B) db.students.aggregate({ $count: "total" })
C) db.students.aggregate([{ $group: { total: { $sum: 1 } } }])
D) db.students.find().count()

Answer: A) db.students.count()
Explanation: The count() method returns the total number of documents in the collection.


98. How would you add a new student with a specified name, grade, and class_id?

A) db.students.insert({ name: "John", grade: "A", class_id: 101 })
B) db.students.insertOne({ name: "John", grade: "A", class_id: 101 })
C) db.students.create({ name: "John", grade: "A", class_id: 101 })
D) db.students.add({ name: "John", grade: "A", class_id: 101 })

Answer: B) db.students.insertOne({ name: "John", grade: "A", class_id: 101 })
Explanation: The insertOne() method adds a new document to the collection.


99. Which method is used to check if a field exists in a document?

A) $exists
B) $isset
C) $check
D) $presence

Answer: A) $exists
Explanation: The $exists operator checks whether a field is present in a document.


100. How do you sort students by their age in ascending order?

A) db.students.find().sort({ age: 1 })
B) db.students.find().sort({ age: -1 })
C) db.students.sort({ age: 1 })
D) db.students.sort({ age: "asc" })

Answer: A) db.students.find().sort({ age: 1 })
Explanation: The sort() method with 1 sorts the results in ascending order.


Tags

Post a Comment

0Comments

Post a Comment (0)

About Me