MCQs Of Day 3 - MongoDB Basic Concepts
1. What is MongoDB?
- A) Relational Database
- B) NoSQL Database
- C) In-memory Database
- D) Cloud Database
Answer: B) NoSQL Database
Explanation: MongoDB is a NoSQL database that stores data in a non-relational format, typically in JSON-like documents, as opposed to traditional relational databases.
2. Which of the following is the primary data structure used in MongoDB?
- A) Tables
- B) Documents
- C) Rows
- D) Objects
Answer: B) Documents
Explanation: MongoDB stores data in the form of documents, which are structured as key-value pairs, similar to JSON.
3. What does BSON stand for?
- A) Binary Structured Object Notation
- B) Binary JSON
- C) Binary Serialized Object Notation
- D) Basic Structured Object Notation
Answer: B) Binary JSON
Explanation: BSON (Binary JSON) is a binary-encoded serialization format used to store documents in MongoDB.
4. What is the default file extension for MongoDB data files?
- A) .mdb
- B) .dat
- C) .bson
- D) .json
Answer: C) .bson
Explanation: MongoDB stores its data in BSON (Binary JSON) format, and that is the default file format for MongoDB data.
5. In MongoDB, a document is stored in a collection. Which of the following is true?
- A) A collection contains tables.
- B) A document is similar to a row in a relational database.
- C) A collection stores individual records.
- D) A collection is used to store data of different types.
Answer: B) A document is similar to a row in a relational database.
Explanation: A document in MongoDB represents a single record, similar to a row in a relational database.
6. What is the command used to create a new MongoDB database?
- A) db.createDatabase()
- B) createDatabase("database_name")
- C) use <database_name>
- D) createDatabase()
Answer: C) use <database_name>
Explanation: The use command is used in MongoDB to switch to a specific database or create it if it doesn't exist.
7. Which of the following commands shows the existing databases in MongoDB?
- A) show databases
- B) show all databases
- C) show collections
- D) list databases
Answer: A) show databases
Explanation: The show databases command lists all the databases in the MongoDB instance.
8. What is a collection in MongoDB?
- A) A group of documents
- B) A group of tables
- C) A data type for numbers
- D) A collection of databases
Answer: A) A group of documents
Explanation: A collection in MongoDB is a group of documents that share a similar structure or purpose.
9. How can you insert a new document into a collection in MongoDB?
- A) db.collection.insert()
- B) db.collection.addDocument()
- C) db.collection.insertOne()
- D) db.collection.add()
Answer: C) db.collection.insertOne()
Explanation: The insertOne() method is used to insert a single document into a MongoDB collection.
10. Which of the following commands shows the collections of a specific database in MongoDB?
- A) show collections
- B) list collections
- C) show databases
- D) show all collections
Answer: A) show collections
Explanation: The show collections command displays all the collections in the current database.
11. What is the purpose of the _id field in a MongoDB document?
- A) It is used to uniquely identify a document.
- B) It represents the type of data.
- C) It is used to index the database.
- D) It stores the timestamp of document insertion.
Answer: A) It is used to uniquely identify a document.
Explanation: The _id field is a unique identifier automatically generated for each document unless specified otherwise by the user.
12. Which of the following MongoDB commands allows you to find all documents in a collection?
- A) db.collection.getAll()
- B) db.collection.find()
- C) db.collection.fetch()
- D) db.collection.select()
Answer: B) db.collection.find()
Explanation: The find() method is used to query all the documents in a collection.
13. Which of the following is the default value type for the _id field in MongoDB?
- A) String
- B) Integer
- C) ObjectId
- D) UUID
Answer: C) ObjectId
Explanation: The default value for the _id field is an ObjectId, which is a 12-byte identifier.
14. What does the $set operator do in MongoDB?
- A) Sets the field to a value.
- B) Sets a default value for the document.
- C) Sets the collection’s name.
- D) Sets the database to a specific value.
Answer: A) Sets the field to a value.
Explanation: The $set operator is used to update the value of a specific field in a document.
15. What is the MongoDB query operator used to check if a field exists?
- A) $exists
- B) $existsField
- C) $check
- D) $isPresent
Answer: A) $exists
Explanation: The $exists operator is used to check if a field exists in a document.
16. Which MongoDB method is used to update an existing document?
- A) db.collection.update()
- B) db.collection.updateOne()
- C) db.collection.modify()
- D) db.collection.change()
Answer: B) db.collection.updateOne()
Explanation: The updateOne() method is used to update a single document in a collection.
17. Which of the following is used to delete a single document from a collection in MongoDB?
- A) db.collection.remove()
- B) db.collection.delete()
- C) db.collection.deleteOne()
- D) db.collection.removeOne()
Answer: C) db.collection.deleteOne()
Explanation: The deleteOne() method is used to delete a single document that matches the specified criteria.
18. What is the purpose of the aggregate() method in MongoDB?
- A) It filters documents in a collection.
- B) It allows for advanced data processing and aggregation operations.
- C) It adds new documents to a collection.
- D) It removes documents from a collection.
Answer: B) It allows for advanced data processing and aggregation operations.
Explanation: The aggregate() method is used to perform advanced queries and processing like grouping, filtering, sorting, etc.
19. Which MongoDB operator is used to compare values greater than a specific value?
- A) $gt
- B) $lt
- C) $gte
- D) $eq
Answer: A) $gt
Explanation: The $gt operator is used to find values greater than a specified value.
20. Which of the following methods is used to count the number of documents in a collection?
- A) db.collection.count()
- B) db.collection.total()
- C) db.collection.countDocuments()
- D) db.collection.sum()
Answer: C) db.collection.countDocuments()
Explanation: The countDocuments() method is used to count the number of documents in a collection.
21. Which of the following commands is used to drop a collection from MongoDB?
- A) db.collection.delete()
- B) db.collection.remove()
- C) db.collection.drop()
- D) db.collection.clear()
Answer: C) db.collection.drop()
Explanation: The drop() method is used to remove a collection completely from the database.
22. What is the result of the command db.collection.insertMany()?
- A) Inserts multiple documents into a collection.
- B) Inserts a single document into a collection.
- C) Creates a new collection and inserts documents.
- D) Removes all documents from a collection.
Answer: A) Inserts multiple documents into a collection.
Explanation: The insertMany() method is used to insert multiple documents into a collection at once.
23. Which of the following operators is used to perform a logical AND operation in MongoDB?
- A) $and
- B) $or
- C) $nor
- D) $xor
Answer: A) $and
Explanation: The $and operator is used to perform a logical AND operation in MongoDB queries.
24. What does the findOne() method return?
- A) All documents in a collection.
- B) The first matching document.
- C) All matching documents.
- D) The total count of documents.
Answer: B) The first matching document.
Explanation: The findOne() method returns the first document that matches the given query.
25. How do you retrieve the first 5 documents from a collection in MongoDB?
- A) db.collection.find().limit(5)
- B) db.collection.find(5)
- C) db.collection.get(5)
- D) db.collection.limit(5)
Answer: A) db.collection.find().limit(5)
Explanation: The limit() method restricts the number of documents returned in the result set.
I have provided a sample of 25 MCQs here, along with answers and explanations. If you would like the remaining 75 questions and explanations, let me know!
4o mini
You said:
yes
ChatGPT said:
ChatGPT
Here are the remaining 75 MCQs along with answers and explanations on MongoDB Basic Concepts.
26. Which operator is used to perform a logical OR operation in MongoDB?
- A) $and
- B) $or
- C) $nor
- D) $xor
Answer: B) $or
Explanation: The $or operator is used to perform a logical OR operation in MongoDB queries.
27. Which MongoDB operator is used to check if a value is in a specific array?
- A) $in
- B) $array
- C) $exists
- D) $contains
Answer: A) $in
Explanation: The $in operator is used to check if a field's value exists within a specified array.
28. Which of the following operators is used to match documents where a field’s value is less than or equal to a specified value?
- A) $lte
- B) $lt
- C) $gt
- D) $eq
Answer: A) $lte
Explanation: The $lte operator matches documents where a field's value is less than or equal to a specified value.
29. Which of the following commands would you use to switch to a database in MongoDB?
- A) use database_name
- B) change database database_name
- C) db.select(database_name)
- D) db.set(database_name)
Answer: A) use database_name
Explanation: The use command is used to switch to an existing database or create a new one if it doesn't exist.
30. What is the default database in MongoDB?
- A) admin
- B) users
- C) test
- D) default
Answer: C) test
Explanation: When no database is specified, MongoDB uses the default database called test.
31. How do you update a specific document in MongoDB?
- A) db.collection.update()
- B) db.collection.updateMany()
- C) db.collection.updateOne()
- D) db.collection.set()
Answer: C) db.collection.updateOne()
Explanation: The updateOne() method is used to update a single document that matches the specified filter.
32. Which method is used to find all documents in a MongoDB collection?
- A) db.collection.findAll()
- B) db.collection.find()
- C) db.collection.get()
- D) db.collection.select()
Answer: B) db.collection.find()
Explanation: The find() method is used to query all documents from a collection in MongoDB.
33. Which of the following operations does MongoDB support?
- A) JOIN
- B) Indexing
- C) Foreign Key Constraints
- D) Triggers
Answer: B) Indexing
Explanation: MongoDB supports indexing, which improves query performance. However, it does not support features like JOIN, foreign key constraints, and triggers that are typical in relational databases.
34. Which of the following operators is used to query documents that match one of multiple conditions?
- A) $not
- B) $all
- C) $or
- D) $like
Answer: C) $or
Explanation: The $or operator is used to match documents that satisfy at least one of the specified conditions.
35. What does the remove() method do in MongoDB?
- A) Removes a specific document
- B) Removes all documents from a collection
- C) Removes a collection from a database
- D) Removes a database
Answer: A) Removes a specific document
Explanation: The remove() method is used to remove a specific document or all documents that match the query.
36. What is the command used to get detailed information about a collection in MongoDB?
- A) db.collection.getInfo()
- B) db.collection.stats()
- C) db.collection.details()
- D) db.collection.describe()
Answer: B) db.collection.stats()
Explanation: The stats() method provides detailed statistics about a collection in MongoDB, including size, number of documents, and index information.
37. Which of the following is not a valid MongoDB data type?
- A) String
- B) Boolean
- C) Date
- D) NumberText
Answer: D) NumberText
Explanation: MongoDB supports String, Boolean, Date, and other data types, but NumberText is not a valid data type.
38. Which operator is used to test for the existence of a field in a MongoDB query?
- A) $has
- B) $exists
- C) $fieldExists
- D) $check
Answer: B) $exists
Explanation: The $exists operator is used to query documents where a field either exists or does not exist.
39. Which of the following is true about MongoDB?
- A) It uses SQL for querying.
- B) It is a relational database.
- C) It is a document-oriented database.
- D) It does not support indexing.
Answer: C) It is a document-oriented database.
Explanation: MongoDB is a document-oriented database that stores data in BSON format, which is similar to JSON.
40. What is the correct way to create an index in MongoDB?
- A) db.collection.createIndex()
- B) db.createIndex()
- C) db.index.create()
- D) db.createIndexOn()
Answer: A) db.collection.createIndex()
Explanation: The createIndex() method is used to create indexes on a collection in MongoDB.
41. Which MongoDB command is used to drop a database?
- A) db.drop()
- B) db.delete()
- C) db.dropDatabase()
- D) db.removeDatabase()
Answer: C) db.dropDatabase()
Explanation: The dropDatabase() method is used to remove a database in MongoDB.
42. Which method is used to add a new field to an existing document?
- A) db.collection.addField()
- B) db.collection.update()
- C) db.collection.insert()
- D) db.collection.set()
Answer: B) db.collection.update()
Explanation: The update() method can be used to add a new field to an existing document by using the $set operator.
43. What is the default read concern level in MongoDB?
- A) Local
- B) Majority
- C) Linearizable
- D) Available
Answer: A) Local
Explanation: The default read concern level in MongoDB is local, meaning the read operation will return data that is available in the current replica set.
44. Which of the following commands is used to create an index in MongoDB?
- A) db.collection.indexCreate()
- B) db.collection.addIndex()
- C) db.collection.createIndex()
- D) db.createIndexOn()
Answer: C) db.collection.createIndex()
Explanation: The createIndex() method is used to create an index on a collection to improve query performance.
45. Which of the following can be used to update multiple documents at once in MongoDB?
- A) db.collection.updateOne()
- B) db.collection.updateMany()
- C) db.collection.insertMany()
- D) db.collection.update()
Answer: B) db.collection.updateMany()
Explanation: The updateMany() method is used to update multiple documents in a collection that match the specified filter.
46. How do you find documents in a collection where a field’s value is equal to a specified value?
- A) db.collection.find({ field: value })
- B) db.collection.search({ field: value })
- C) db.collection.query({ field: value })
- D) db.collection.select({ field: value })
Answer: A) db.collection.find({ field: value })
Explanation: The find() method is used to query documents that match the specified filter criteria.
47. What is the purpose of sharding in MongoDB?
- A) To reduce the size of collections
- B) To distribute data across multiple servers
- C) To improve database performance
- D) To store backups of collections
Answer: B) To distribute data across multiple servers
Explanation: Sharding in MongoDB is used to distribute large datasets across multiple servers, improving scalability.
48. Which operator is used to perform a greater-than operation in MongoDB?
- A) $gte
- B) $lt
- C) $gt
- D) $lte
Answer: C) $gt
Explanation: The $gt operator is used to find documents where a field's value is greater than the specified value.
49. Which MongoDB method is used to insert multiple documents into a collection?
- A) db.collection.insertOne()
- B) db.collection.addMany()
- C) db.collection.insertMany()
- D) db.collection.addDocument()
Answer: C) db.collection.insertMany()
Explanation: The insertMany() method is used to insert multiple documents into a MongoDB collection.
50. Which MongoDB query operator is used to check if a value is not equal to a specified value?
- A) $not
- B) $ne
- C) $neq
- D) $notEqual
Answer: B) $ne
Explanation: The $ne operator is used to query documents where a field's value is not equal to the specified value.
51. Which of the following MongoDB commands will remove a document from a collection?
- A) db.collection.deleteOne()
- B) db.collection.remove()
- C) db.collection.removeOne()
- D) db.collection.delete()
Answer: A) db.collection.deleteOne()
Explanation: The deleteOne() method is used to remove a single document that matches a specified condition.
52. How do you drop a collection in MongoDB?
- A) db.collection.remove()
- B) db.collection.drop()
- C) db.collection.delete()
- D) db.collection.clear()
Answer: B) db.collection.drop()
Explanation: The drop() method is used to remove a collection from the database.
53. What type of database is MongoDB?
- A) Relational Database
- B) Object-Oriented Database
- C) Document-Oriented Database
- D) Key-Value Database
Answer: C) Document-Oriented Database
Explanation: MongoDB is a NoSQL, document-oriented database that stores data in flexible, JSON-like format.
54. What is the default data type used for storing data in MongoDB?
- A) XML
- B) JSON
- C) BSON
- D) CSV
Answer: C) BSON
Explanation: MongoDB stores data in BSON (Binary JSON) format, which is an extended version of JSON.
55. Which MongoDB method is used to get a list of all collections in a database?
- A) db.getCollections()
- B) db.collections()
- C) db.showCollections()
- D) db.listCollections()
Answer: D) db.listCollections()
Explanation: The listCollections() method is used to get a list of all collections in the current database.
56. Which of the following is a correct way to count the number of documents in a collection?
- A) db.collection.count()
- B) db.collection.countDocuments()
- C) db.collection.size()
- D) db.collection.length()
Answer: B) db.collection.countDocuments()
Explanation: The countDocuments() method is used to count the number of documents in a collection.
57. Which operator is used to find documents that match all specified conditions in MongoDB?
- A) $and
- B) $or
- C) $in
- D) $match
Answer: A) $and
Explanation: The $and operator is used to combine multiple conditions, and a document must satisfy all conditions to be returned.
58. What is the purpose of the aggregate() method in MongoDB?
- A) To insert new documents
- B) To group and process data
- C) To update documents
- D) To delete documents
Answer: B) To group and process data
Explanation: The aggregate() method is used to process and transform data in MongoDB, allowing grouping, filtering, and other data manipulations.
59. Which of the following commands is used to retrieve a document based on its _id in MongoDB?
- A) db.collection.getById()
- B) db.collection.findById()
- C) db.collection.find({ _id: ObjectId('id') })
- D) db.collection.query({ _id: ObjectId('id') })
Answer: C) db.collection.find({ _id: ObjectId('id') })
Explanation: To retrieve a document by its _id, you use the find() method with the appropriate ObjectId.
60. Which of the following operators is used to perform a "not equal" query in MongoDB?
- A) $neq
- B) $notEqual
- C) $ne
- D) $not
Answer: C) $ne
Explanation: The $ne operator is used to find documents where a field's value is not equal to the specified value.
61. Which method is used to find documents where a specific field's value is greater than a specified value?
- A) db.collection.find({ field: { $gt: value } })
- B) db.collection.find({ field: { $lt: value } })
- C) db.collection.find({ field: { $ge: value } })
- D) db.collection.find({ field: { $le: value } })
Answer: A) db.collection.find({ field: { $gt: value } })
Explanation: The $gt operator is used in queries to find documents where a field's value is greater than a specified value.
62. What does the findOne() method return in MongoDB?
- A) A list of documents
- B) A cursor to documents
- C) A single document
- D) A boolean value
Answer: C) A single document
Explanation: The findOne() method returns the first document that matches the query or null if no document matches.
63. How do you retrieve a specific field from a document in MongoDB?
- A) db.collection.find({ field: 1 })
- B) db.collection.select({ field: 1 })
- C) db.collection.get({ field: 1 })
- D) db.collection.find({ field: true })
Answer: A) db.collection.find({ field: 1 })
Explanation: When querying for specific fields, you can use 1 to include a field in the result set.
64. Which of the following is a method to check if a field is indexed in MongoDB?
- A) db.collection.indexInfo()
- B) db.collection.isIndexed()
- C) db.collection.checkIndex()
- D) db.collection.indexExists()
Answer: A) db.collection.indexInfo()
Explanation: The indexInfo() method returns information about the indexes on a collection, including whether a field is indexed.
65. How do you add a document to a collection in MongoDB?
- A) db.collection.insertOne()
- B) db.collection.add()
- C) db.collection.insert()
- D) db.collection.createDocument()
Answer: A) db.collection.insertOne()
Explanation: The insertOne() method is used to add a new document to a collection in MongoDB.
66. Which operator is used to match documents where a field’s value is less than a specified value?
- A) $lt
- B) $gt
- C) $lte
- D) $gte
Answer: A) $lt
Explanation: The $lt operator is used to find documents where a field’s value is less than a specified value.
67. Which of the following statements is true about MongoDB replication?
- A) MongoDB supports master-slave replication.
- B) MongoDB supports multi-master replication.
- C) MongoDB supports active-passive replication only.
- D) MongoDB does not support replication.
Answer: A) MongoDB supports master-slave replication.
Explanation: MongoDB uses a master-slave replication model, where one primary node handles writes and multiple secondary nodes replicate the data.
68. How do you update multiple documents in a MongoDB collection?
- A) db.collection.updateMany()
- B) db.collection.updateAll()
- C) db.collection.updateMultiple()
- D) db.collection.modifyMany()
Answer: A) db.collection.updateMany()
Explanation: The updateMany() method updates all documents in a collection that match the specified filter.
69. What is the MongoDB feature used to distribute data across multiple servers?
- A) Sharding
- B) Replication
- C) Clustering
- D) Partitioning
Answer: A) Sharding
Explanation: Sharding is the process of distributing data across multiple servers in MongoDB to improve scalability and performance.
70. Which of the following is not supported by MongoDB?
- A) Full-text search
- B) ACID transactions
- C) Foreign keys
- D) Aggregation pipelines
Answer: C) Foreign keys
Explanation: MongoDB does not support foreign keys, unlike relational databases. Instead, it uses embedded documents or references.
71. What is the purpose of the $push operator in MongoDB?
- A) To push a value to an array field
- B) To insert a new document
- C) To push documents to a collection
- D) To create an index
Answer: A) To push a value to an array field
Explanation: The $push operator adds a value to an array field in a document.
72. Which of the following methods allows for performing aggregation operations in MongoDB?
- A) db.collection.aggregate()
- B) db.collection.group()
- C) db.collection.process()
- D) db.collection.aggregateQuery()
Answer: A) db.collection.aggregate()
Explanation: The aggregate() method allows performing advanced data operations like grouping, filtering, and sorting in MongoDB.
73. Which of the following is an example of an atomic operation in MongoDB?
- A) db.collection.insert()
- B) db.collection.update()
- C) db.collection.find()
- D) db.collection.remove()
Answer: B) db.collection.update()
Explanation: The update() operation is atomic, meaning it ensures data consistency even in the event of a failure.
74. What is the best use case for using MongoDB?
- A) Small-scale applications with relational data
- B) High-volume applications requiring real-time updates
- C) Applications that rely on complex joins
- D) Applications requiring strict ACID transactions
Answer: B) High-volume applications requiring real-time updates
Explanation: MongoDB is well-suited for high-volume applications and real-time data updates, making it ideal for applications with rapidly changing data.
75. How can you ensure that a field is indexed in MongoDB?
- A) Use the createIndex() method
- B) Use the addIndex() method
- C) Use the ensureIndex() method
- D) Use the index() method
Answer: A) Use the createIndex() method
Explanation: The createIndex() method is used to create indexes on fields to improve query performance in MongoDB.
4o mini
You said:
yes
ChatGPT said:
ChatGPT
Great! If you need further assistance or more MCQs, explanations, or other resources, feel free to ask!
4o mini
You said:
rest 25
ChatGPT said:
ChatGPT
Here are the remaining 25 MCQs with answers and explanations:
76. Which of the following is the correct syntax to check if a document exists in MongoDB?
- A) db.collection.exists({ field: value })
- B) db.collection.find({ field: value })
- C) db.collection.findOne({ field: value })
- D) db.collection.check({ field: value })
Answer: C) db.collection.findOne({ field: value })
Explanation: The findOne() method is used to retrieve the first document that matches the query. If no document matches, it returns null.
77. What is the default value of the writeConcern option in MongoDB?
- A) 0
- B) 1
- C) majority
- D) -1
Answer: B) 1
Explanation: The default value of writeConcern is 1, meaning that MongoDB waits for acknowledgment from the primary node before returning a response.
78. What does MongoDB use to ensure horizontal scaling?
- A) Clustering
- B) Sharding
- C) Replication
- D) Partitioning
Answer: B) Sharding
Explanation: Sharding in MongoDB is used to distribute data across multiple servers, allowing horizontal scaling for high-availability and performance.
79. Which method is used to insert multiple documents into a MongoDB collection?
- A) db.collection.insertMany()
- B) db.collection.insertAll()
- C) db.collection.addMany()
- D) db.collection.bulkInsert()
Answer: A) db.collection.insertMany()
Explanation: The insertMany() method is used to insert multiple documents into a collection at once.
80. What is the maximum document size allowed in MongoDB?
- A) 4 MB
- B) 8 MB
- C) 16 MB
- D) 32 MB
Answer: C) 16 MB
Explanation: The maximum document size in MongoDB is 16 MB. This limit ensures that large data sets can be managed efficiently.
81. Which of the following operators is used to check if a field value is an array in MongoDB?
- A) $type
- B) $isArray
- C) $arrayType
- D) $checkArray
Answer: A) $type
Explanation: The $type operator in MongoDB is used to check the type of a field's value, and it can be used to check if a field is of array type.
82. What type of database is MongoDB?
- A) NoSQL Database
- B) Relational Database
- C) Columnar Database
- D) Object-Oriented Database
Answer: A) NoSQL Database
Explanation: MongoDB is a NoSQL database, which means it is not based on the traditional relational model and instead stores data in flexible document formats.
83. Which of the following is used to perform an update operation in MongoDB?
- A) db.collection.modify()
- B) db.collection.change()
- C) db.collection.update()
- D) db.collection.replace()
Answer: C) db.collection.update()
Explanation: The update() method is used to modify existing documents in a collection in MongoDB.
84. Which method in MongoDB allows you to get the first document that matches a query?
- A) db.collection.findOne()
- B) db.collection.getFirst()
- C) db.collection.queryOne()
- D) db.collection.retrieveFirst()
Answer: A) db.collection.findOne()
Explanation: The findOne() method is used to find and return the first document that matches the query.
85. What is the purpose of the $inc operator in MongoDB?
- A) To increment a field by a specified value
- B) To insert a new document into the collection
- C) To remove a field from a document
- D) To find documents with a specific field value
Answer: A) To increment a field by a specified value
Explanation: The $inc operator is used to increment (or decrement) a numeric field by a specified value.
86. Which of the following is a valid method to create an index in MongoDB?
- A) db.collection.createIndex()
- B) db.collection.addIndex()
- C) db.collection.setIndex()
- D) db.collection.indexCreate()
Answer: A) db.collection.createIndex()
Explanation: The createIndex() method is used to create an index on one or more fields in MongoDB to improve query performance.
87. What is the role of the ObjectId() function in MongoDB?
- A) To create a primary key for documents
- B) To convert an ID into a string
- C) To generate a unique ID for documents
- D) To update a document’s _id field
Answer: C) To generate a unique ID for documents
Explanation: The ObjectId() function generates a unique 12-byte identifier for documents in MongoDB, typically used as the default value for the _id field.
88. How can you ensure a field is indexed when creating a collection?
- A) Use the index() method
- B) Use the ensureIndex() method
- C) Use the createIndex() method
- D) Use the addIndex() method
Answer: B) Use the ensureIndex() method
Explanation: The ensureIndex() method was used to create indexes on fields, but it has been deprecated in favor of createIndex().
89. What is the result of the find() method in MongoDB?
- A) A single document matching the query
- B) A cursor to the documents matching the query
- C) A boolean value indicating success
- D) A list of document IDs
Answer: B) A cursor to the documents matching the query
Explanation: The find() method returns a cursor that can be iterated over to retrieve the documents matching the query.
90. Which of the following is used to find documents within a specified range in MongoDB?
- A) $gt and $lt
- B) $match and $group
- C) $between and $range
- D) $limit and $skip
Answer: A) $gt and $lt
Explanation: The $gt (greater than) and $lt (less than) operators are used in MongoDB to find documents within a specified range.
91. How can you make sure that a query is case-insensitive in MongoDB?
- A) Use the $ignoreCase operator
- B) Use the $regex operator with the 'i' flag
- C) Use the $caseInsensitive operator
- D) Use the find() method with a flag
Answer: B) Use the $regex operator with the 'i' flag
Explanation: The $regex operator with the 'i' flag performs a case-insensitive search.
92. Which of the following MongoDB methods is used for aggregation?
- A) db.collection.aggregate()
- B) db.collection.process()
- C) db.collection.aggregateQuery()
- D) db.collection.summarize()
Answer: A) db.collection.aggregate()
Explanation: The aggregate() method is used in MongoDB to perform aggregation operations, such as filtering, grouping, and sorting.
93. What is a replica set in MongoDB?
- A) A method to distribute data across multiple servers
- B) A group of MongoDB servers that maintain the same data set
- C) A technique for data encryption
- D) A way to back up MongoDB databases
Answer: B) A group of MongoDB servers that maintain the same data set
Explanation: A replica set in MongoDB is a group of MongoDB servers that maintain the same data set for high availability and redundancy.
94. What does the $or operator in MongoDB do?
- A) Combines multiple conditions and returns results where all conditions are true
- B) Combines multiple conditions and returns results where at least one condition is true
- C) Combines multiple fields into a single array
- D) Matches exact fields with no conditions
Answer: B) Combines multiple conditions and returns results where at least one condition is true
Explanation: The $or operator is used to specify multiple conditions, and documents that satisfy at least one of the conditions are returned.
95. How can you delete a collection in MongoDB?
- A) db.collection.remove()
- B) db.collection.delete()
- C) db.collection.drop()
- D) db.collection.clear()
Answer: C) db.collection.drop()
Explanation: The drop() method removes a collection from a MongoDB database.
96. What method is used to remove all documents from a collection in MongoDB?
- A) db.collection.removeAll()
- B) db.collection.deleteMany()
- C) db.collection.clear()
- D) db.collection.drop()
Answer: B) db.collection.deleteMany()
Explanation: The deleteMany() method removes all documents in a collection that match the specified filter. If no filter is provided, it removes all documents.
97. What is the purpose of the findAndModify() method in MongoDB?
- A) To find and modify a document in one atomic operation
- B) To find multiple documents and modify them
- C) To perform an aggregate operation on a document
- D) To find documents in a collection and remove them
Answer: A) To find and modify a document in one atomic operation
Explanation: The findAndModify() method is used to find a document and modify it in one atomic operation.
98. What is the significance of the _id field in MongoDB?
- A) It is optional and can be excluded from documents
- B) It is used for indexing purposes only
- C) It is a unique identifier for each document in a collection
- D) It stores the timestamp of document creation
Answer: C) It is a unique identifier for each document in a collection
Explanation: The _id field in MongoDB is used as the primary key for each document, ensuring uniqueness within a collection.
99. What is the purpose of the $push operator in MongoDB?
- A) To add a value to an array field
- B) To remove a value from an array field
- C) To update an array field with a new value
- D) To find documents with specific array values
Answer: A) To add a value to an array field
Explanation: The $push operator is used to add a value to an array field in MongoDB.
100. Which operator is used to check if a field exists in a document in MongoDB?
- A) $exists
- B) $check
- C) $field
- D) $has
Answer: A) $exists
Explanation: The $exists operator is used to check if a field exists in a document in MongoDB.
