MCQs Of Day 25: MongoDB Administration (Advanced)
1. What command is used to enable database profiling in MongoDB?
a) db.setProfilingLevel(1)
b) db.enableProfiling(true)
c) db.enableProfiling(1)
d) db.setProfilingStatus(1)
Answer: a) db.setProfilingLevel(1)
Explanation: The correct syntax to enable profiling with level 1 is db.setProfilingLevel(1), where 1 enables profiling for slow queries.
2. Which field in the system.profile collection contains the execution time of a query?
a) exec_time
b) millis
c) duration
d) time_taken
Answer: b) millis
Explanation: The millis field in system.profile represents the execution time of a query in milliseconds.
3. How can you disable MongoDB database profiling?
a) db.setProfilingLevel(0)
b) db.disableProfiling()
c) db.setProfilingLevel(2)
d) db.stopProfiling()
Answer: a) db.setProfilingLevel(0)
Explanation: db.setProfilingLevel(0) disables profiling in MongoDB.
4. What is the default value for profiling level in MongoDB?
a) 0
b) 1
c) 2
d) None
Answer: a) 0
Explanation: The default profiling level in MongoDB is 0, which means profiling is turned off.
5. What MongoDB command is used to view the content of the system.profile collection?
a) db.profile.find()
b) db.system.profile.find()
c) db.profiledata.find()
d) db.system.stats.find()
Answer: b) db.system.profile.find()
Explanation: To view the profiling data, you query the system.profile collection with db.system.profile.find().
6. What is the purpose of the slowms option in db.setProfilingLevel()?
a) It sets the maximum number of queries to profile.
b) It sets the threshold time for profiling queries that take longer than a certain duration.
c) It specifies the profiling level for database operations.
d) It controls the number of queries to log in the system.profile collection.
Answer: b) It sets the threshold time for profiling queries that take longer than a certain duration.
Explanation: The slowms option defines the time in milliseconds after which a query is considered slow and profiled.
7. How can you improve query performance if a slow query is being logged?
a) Change the query to use multiple conditions.
b) Add appropriate indexes to the queried fields.
c) Reduce the number of documents in the collection.
d) Use db.dropDatabase() to reset the database.
Answer: b) Add appropriate indexes to the queried fields.
Explanation: Adding indexes on the fields used in the query can significantly improve query performance.
8. Which command is used to create an index on a field in MongoDB?
a) db.createIndex({field: 1})
b) db.createIndex("field")
c) db.collection.createIndex({field: 1})
d) db.index.create({field: 1})
Answer: c) db.collection.createIndex({field: 1})
Explanation: The correct syntax to create an index on a field is db.collection.createIndex({field: 1}).
9. How can you check for slow queries in MongoDB logs?
a) Search for the term error in the logs.
b) Search for the term replication in the logs.
c) Look for entries in system.profile.
d) Search for entries with execution time greater than the threshold in MongoDB logs.
Answer: d) Search for entries with execution time greater than the threshold in MongoDB logs.
Explanation: Slow queries are logged with their execution times, which can be checked in the logs or system.profile.
10. Which of the following can be considered an indicator of a replication issue in MongoDB logs?
a) No space left on device
b) Replication lag detected
c) Index creation failed
d) Memory usage exceeded
Answer: b) Replication lag detected
Explanation: Replication lag occurs when the secondary nodes are not synchronized with the primary node, and it is logged as Replication lag detected.
11. Which log level provides the most detailed logging in MongoDB?
a) 0
b) 1
c) 2
d) 3
Answer: d) 3
Explanation: Log level 3 provides the most detailed logging, including information for debugging.
12. What is the MongoDB command used to change log verbosity?
a) db.setLogLevel()
b) db.setLogVerbosity()
c) mongod --verbosity 2
d) mongod --logLevel 3
Answer: c) mongod --verbosity 2
Explanation: You can change the log verbosity level by running mongod with the --verbosity option.
13. What command would you use to monitor MongoDB’s performance using mongostat?
a) mongostat -h localhost
b) mongostat --host localhost
c) mongostat --performance
d) mongostat --metrics
Answer: b) mongostat --host localhost
Explanation: mongostat monitors performance statistics for MongoDB. Using the --host option allows you to specify the MongoDB instance.
14. What does mongotop monitor in MongoDB?
a) Memory usage
b) Read and write operations per collection
c) Database size
d) Query execution times
Answer: b) Read and write operations per collection
Explanation: mongotop is used to monitor the time spent reading and writing to each collection in MongoDB.
15. What is the best way to resolve high query execution time in MongoDB?
a) Increase the size of the database.
b) Add more RAM to the MongoDB server.
c) Create indexes on frequently queried fields.
d) Reduce the number of records in the database.
Answer: c) Create indexes on frequently queried fields.
Explanation: Creating indexes on frequently queried fields optimizes query execution time by reducing the amount of data scanned.
16. What does the millis field in the system.profile collection represent?
a) The total number of queries executed.
b) The time a query took to execute in milliseconds.
c) The number of milliseconds before a query starts.
d) The number of queries logged during profiling.
Answer: b) The time a query took to execute in milliseconds.
Explanation: The millis field contains the execution time of a query in milliseconds.
17. Which of the following is the default value of logRotate in MongoDB configuration?
a) none
b) rename
c) delete
d) rotate
Answer: b) rename
Explanation: By default, MongoDB uses the rename option for log rotation, which renames old logs and starts a new log file.
18. What MongoDB configuration option allows log rotation based on size?
a) logRotationSize
b) logFileSizeLimit
c) logRotate
d) size
Answer: d) size
Explanation: The size option in MongoDB configuration specifies the maximum size of a log file before it is rotated.
19. What would be a possible reason for replication lag in MongoDB?
a) Network issues between the primary and secondary nodes.
b) Insufficient RAM on the primary node.
c) A bug in the MongoDB version.
d) Incorrect database indexing.
Answer: a) Network issues between the primary and secondary nodes.
Explanation: Replication lag can occur if there are network issues between the primary and secondary nodes, preventing data from being replicated in real-time.
20. Which command is used to manually initiate a MongoDB replica set sync?
a) rs.sync()
b) rs.syncFrom()
c) rs.reSync()
d) rs.replicate()
Answer: b) rs.syncFrom()
Explanation: rs.syncFrom() allows a secondary node to sync from a specific node in the replica set.
21. How can you check the current status of a MongoDB replica set?
a) rs.status()
b) db.replSetStatus()
c) db.rsStatus()
d) rs.info()
Answer: a) rs.status()
Explanation: The rs.status() command is used to check the status of a MongoDB replica set.
22. What is the role of the primary node in a MongoDB replica set?
a) It handles all read operations.
b) It is used only for writing operations.
c) It can handle both reads and writes.
d) It handles write operations and replication.
Answer: c) It can handle both reads and writes.
Explanation: The primary node is the main node in a replica set that handles both read and write operations.
23. How does MongoDB handle a primary node failure in a replica set?
a) It automatically shuts down the replica set.
b) It triggers an election to elect a new primary.
c) It continues using the old primary.
d) It requires manual intervention to elect a new primary.
Answer: b) It triggers an election to elect a new primary.
Explanation: MongoDB automatically triggers an election in case the primary node fails, and a new primary is elected from the secondary nodes.
24. Which of the following is a valid command to add a secondary node to a MongoDB replica set?
a) rs.addSecondary()
b) rs.add()
c) rs.addNode()
d) rs.addHost()
Answer: b) rs.add()
Explanation: The correct command to add a secondary node is rs.add(), which adds a new member to the replica set.
25. In MongoDB, what is the primary purpose of writeConcern?
a) It defines the consistency level of read operations.
b) It defines the durability and acknowledgment level of write operations.
c) It defines the number of replicas to write to.
d) It defines the time taken to write to the database.
Answer: b) It defines the durability and acknowledgment level of write operations.
Explanation: writeConcern determines the level of acknowledgment requested from MongoDB for write operations, ensuring data durability.
26. What is the default value of writeConcern in MongoDB?
a) w: 1
b) w: 0
c) w: "majority"
d) w: 2
Answer: a) w: 1
Explanation: The default writeConcern in MongoDB is w: 1, which means the operation is acknowledged by the primary node only.
27. Which of the following commands is used to monitor real-time database operations in MongoDB?
a) db.monitor()
b) db.operations()
c) mongostat
d) mongosnapshot
Answer: c) mongostat
Explanation: mongostat is used to monitor MongoDB's real-time database operations and performance statistics.
28. What is the purpose of the findAndModify command in MongoDB?
a) To find and modify a document in a single atomic operation.
b) To modify a document and return its original version.
c) To find and modify documents in multiple collections.
d) To find and update multiple documents.
Answer: a) To find and modify a document in a single atomic operation.
Explanation: findAndModify combines a find and modify operation into a single atomic operation in MongoDB.
29. How can you troubleshoot disk space issues in MongoDB?
a) Check the available disk space using df and mongod logs.
b) Rebuild the indexes.
c) Use the compact command to shrink the data.
d) Delete all unused collections.
Answer: a) Check the available disk space using df and mongod logs.
Explanation: Disk space issues can be checked by using disk space commands like df in combination with checking MongoDB logs.
30. What does the oplog refer to in MongoDB replication?
a) The operational log that tracks changes to the data.
b) A log of all the queries executed.
c) The log that tracks write operations in the system.
d) A log that records only successful operations.
Answer: a) The operational log that tracks changes to the data.
Explanation: The oplog is a special capped collection in MongoDB that records all write operations, enabling replication.
31. How do you increase the size of the oplog in MongoDB?
a) Modify the replica set configuration.
b) Modify the mongod.conf file before starting MongoDB.
c) Increase the memory allocated to MongoDB.
d) You cannot increase the size of the oplog.
Answer: b) Modify the mongod.conf file before starting MongoDB.
Explanation: The size of the oplog can be configured by setting the --oplogSize parameter in the mongod.conf file.
32. What does the dbStats command provide in MongoDB?
a) The statistics of the currently running query.
b) General statistics of a MongoDB database such as collection count and storage size.
c) The status of the replication process.
d) Statistics for a particular collection's documents.
Answer: b) General statistics of a MongoDB database such as collection count and storage size.
Explanation: The dbStats command provides general statistics on the database, including the number of collections, documents, and storage size.
33. In MongoDB, how is data written to a capped collection?
a) Data is appended to the collection as new documents.
b) Documents are updated in place.
c) Data is first inserted into the journal and then moved into the collection.
d) Data is written and then copied to secondary nodes.
Answer: a) Data is appended to the collection as new documents.
Explanation: Capped collections are fixed-size collections in MongoDB that maintain insertion order and automatically overwrite the oldest documents when space is needed.
34. Which command is used to delete all documents in a collection without removing the collection itself?
a) db.collection.removeAll()
b) db.collection.drop()
c) db.collection.deleteMany({})
d) db.collection.delete()
Answer: c) db.collection.deleteMany({})
Explanation: db.collection.deleteMany({}) removes all documents in the collection but does not drop the collection itself.
35. How does MongoDB ensure atomicity in operations?
a) Through transaction logs.
b) By using a write lock.
c) Through the use of replica sets.
d) By guaranteeing that all write operations are applied in sequence.
Answer: b) By using a write lock.
Explanation: MongoDB ensures atomicity of write operations by acquiring a write lock on the document, ensuring that no other operation can interfere.
4o mini
You said:
rest 15
ChatGPT said:
ChatGPT
Here are the next 15 questions for your MongoDB quiz:
36. What is the maximum size of a document in MongoDB?
a) 16 MB
b) 4 MB
c) 64 MB
d) 1 GB
Answer: a) 16 MB
Explanation: The maximum size for a single document in MongoDB is 16 MB.
37. What does the explain() method do in MongoDB?
a) It provides an execution plan for a query.
b) It explains how to write queries in MongoDB.
c) It shows the data types of all fields.
d) It explains the replica set configuration.
Answer: a) It provides an execution plan for a query.
Explanation: The explain() method is used to get detailed information about how MongoDB executes a query, including indexes and execution plans.
38. What is the difference between findOne() and find() in MongoDB?
a) findOne() returns multiple documents, while find() returns a single document.
b) findOne() returns only one document that matches the query, while find() returns a cursor for multiple documents.
c) find() is used for aggregation queries, while findOne() is for normal queries.
d) There is no difference.
Answer: b) findOne() returns only one document that matches the query, while find() returns a cursor for multiple documents.
Explanation: findOne() returns a single document, while find() returns a cursor for iterating over multiple documents that match the query.
39. What is the purpose of the aggregate() method in MongoDB?
a) It performs complex queries and transformations on documents.
b) It adds new data to the database.
c) It deletes documents in the database.
d) It indexes documents in the collection.
Answer: a) It performs complex queries and transformations on documents.
Explanation: The aggregate() method is used to process data in a collection and transform it into useful results through various stages such as grouping, sorting, and filtering.
40. What is a shard in MongoDB?
a) A collection that holds all database operations.
b) A node that stores a subset of data in a sharded cluster.
c) A command used to optimize queries.
d) A group of replica sets.
Answer: b) A node that stores a subset of data in a sharded cluster.
Explanation: A shard in MongoDB is a node that holds a subset of the data in a sharded cluster to distribute data across multiple servers.
41. What is the purpose of mongodump in MongoDB?
a) To back up data from a MongoDB database.
b) To delete all data from a MongoDB collection.
c) To update the schema of a MongoDB database.
d) To restart the MongoDB server.
Answer: a) To back up data from a MongoDB database.
Explanation: mongodump is a command-line utility used to create a backup of MongoDB databases.
42. How do you perform a full-text search in MongoDB?
a) Use the textSearch() command.
b) Use a text index and the $text query operator.
c) Use the search() method.
d) MongoDB does not support full-text search.
Answer: b) Use a text index and the $text query operator.
Explanation: MongoDB supports full-text search by creating a text index on the fields and using the $text operator in queries.
43. What is the default storage engine used in MongoDB?
a) WiredTiger
b) MMAPv1
c) In-Memory
d) RocksDB
Answer: a) WiredTiger
Explanation: The default storage engine in MongoDB is WiredTiger, which provides high performance and concurrency support.
44. What is a replica set in MongoDB?
a) A group of servers working together to provide fault tolerance.
b) A backup server for the primary node.
c) A set of secondary nodes in a MongoDB cluster.
d) A collection of documents that share the same schema.
Answer: a) A group of servers working together to provide fault tolerance.
Explanation: A replica set is a group of MongoDB servers that maintain the same data set to ensure high availability and data redundancy.
45. Which of the following commands is used to remove a collection in MongoDB?
a) db.collection.remove()
b) db.collection.drop()
c) db.collection.delete()
d) db.collection.removeAll()
Answer: b) db.collection.drop()
Explanation: The drop() command is used to remove an entire collection from MongoDB, including its data and indexes.
46. What is the purpose of the $set operator in MongoDB?
a) To add a new field to a document.
b) To update an existing field in a document.
c) To replace the value of a field in a document.
d) To delete a field from a document.
Answer: b) To update an existing field in a document.
Explanation: The $set operator is used in update operations to set the value of a field in a document.
47. How does MongoDB handle concurrency control?
a) Through the use of locks and transactions.
b) By limiting the number of read and write operations.
c) Through replication and sharding.
d) By using a single-threaded approach.
Answer: a) Through the use of locks and transactions.
Explanation: MongoDB handles concurrency control using locks and multi-document transactions to ensure data consistency in concurrent operations.
48. What does the getLastError() command in MongoDB do?
a) It retrieves the last error that occurred during an operation.
b) It retrieves the last query result.
c) It retrieves the most recent write operation in the database.
d) It retrieves the last successful operation status.
Answer: a) It retrieves the last error that occurred during an operation.
Explanation: getLastError() is used to retrieve the last error that occurred during a write operation.
49. What is the main advantage of MongoDB over relational databases?
a) MongoDB supports SQL queries.
b) MongoDB supports horizontal scalability with sharding.
c) MongoDB uses transactions for all operations.
d) MongoDB only supports document-oriented data.
Answer: b) MongoDB supports horizontal scalability with sharding.
Explanation: MongoDB offers horizontal scalability through sharding, which allows data to be distributed across multiple servers.
50. How do you perform an atomic operation on a single document in MongoDB?
a) Use the db.collection.update() method with the $set operator.
b) Use the db.collection.findAndModify() method.
c) Use the db.collection.save() method.
d) Use the db.collection.insert() method.
Answer: b) Use the db.collection.findAndModify() method.
Explanation: The findAndModify() method is used to perform atomic operations on a single document, ensuring that the operation is completed in one step.
