MCQs Of Day 24: MongoDB Administration (Basic)

Rashmi Mishra
0

MCQs Of Day 24: MongoDB Administration (Basic)

1. Which command is used to create a new database in MongoDB?

a) db.createDatabase()
b) use <database_name>
c) create <database_name>
d) db.create()

Answer: b) use <database_name>
Explanation: The use <database_name> command is used to switch to a database. If the database does not exist, it is created when data is inserted into it.


2. What is the default storage format for MongoDB databases?

a) JSON
b) BSON
c) XML
d) CSV

Answer: b) BSON
Explanation: MongoDB uses BSON (Binary JSON) to store data, which allows for more complex data structures compared to JSON.


3. Which of the following commands is used to create a collection in MongoDB?

a) createCollection(<collection_name>)
b) db.createCollection(<collection_name>)
c) db.create(<collection_name>)
d) create(<collection_name>)

Answer: b) db.createCollection(<collection_name>)
Explanation: The db.createCollection(<collection_name>) command is used to create a new collection in MongoDB.


4. Which MongoDB method is used to insert multiple documents at once?

a) insertOne()
b) insertMany()
c) addDocuments()
d) pushDocuments()

Answer: b) insertMany()
Explanation: The insertMany() method is used to insert multiple documents into a MongoDB collection.


5. Which command is used to retrieve all documents from a collection in MongoDB?

a) db.collection.get()
b) db.collection.find()
c) db.collection.select()
d) db.collection.query()

Answer: b) db.collection.find()
Explanation: The find() method is used to retrieve all documents from a collection. If no parameters are passed, it returns all documents.


6. What does the updateOne() method do in MongoDB?

a) Updates all documents in a collection
b) Updates the first document matching a filter
c) Inserts a new document
d) Deletes a document

Answer: b) Updates the first document matching a filter
Explanation: The updateOne() method updates a single document that matches the filter criteria.


7. Which operator is used to find documents where the field value is greater than a specified value?

a) $eq
b) $lt
c) $gt
d) $ne

Answer: c) $gt
Explanation: The $gt (greater than) operator is used to find documents where the field value is greater than the specified value.


8. How can you delete a single document from a collection in MongoDB?

a) db.collection.removeOne()
b) db.collection.delete()
c) db.collection.deleteOne()
d) db.collection.remove()

Answer: c) db.collection.deleteOne()
Explanation: The deleteOne() method removes a single document from the collection that matches the filter criteria.


9. Which of the following commands is used to monitor the statistics of a MongoDB database?

a) db.getStats()
b) db.stats()
c) db.collection.stats()
d) db.databaseStats()

Answer: b) db.stats()
Explanation: The db.stats() command returns statistics about the current database, such as size and the number of collections.


10. What command can be used to get statistics about a specific collection?

a) db.collection.stats()
b) db.getCollectionStats()
c) db.stats()
d) db.collection.getStats()

Answer: a) db.collection.stats()
Explanation: The db.collection.stats() command provides detailed statistics about the collection, such as the number of documents, size, and storage used.


11. Which method is used to update multiple documents in MongoDB?

a) updateMany()
b) updateAll()
c) modifyMany()
d) updateDocuments()

Answer: a) updateMany()
Explanation: The updateMany() method updates all documents that match the specified filter criteria.


12. What does the $set operator do in MongoDB?

a) Deletes a field in a document
b) Adds a new field to a document
c) Sets the value of a specified field in a document
d) Adds a new document

Answer: c) Sets the value of a specified field in a document
Explanation: The $set operator is used to set the value of a field in a document, either adding it if it doesn't exist or updating it if it does.


13. Which of the following is a valid way to find all customers older than 30 years?

a) db.customers.find({ age: 30 })
b) db.customers.find({ age: { $lt: 30 } })
c) db.customers.find({ age: { $gt: 30 } })
d) db.customers.find({ age: 30 })

Answer: c) db.customers.find({ age: { $gt: 30 } })
Explanation: The $gt operator is used to find documents where the age is greater than 30.


14. How do you list all the collections in a MongoDB database?

a) show collections
b) list collections
c) db.showCollections()
d) db.list()

Answer: a) show collections
Explanation: The show collections command is used to list all collections in the current database.


15. Which method is used to find the first document that matches a filter in MongoDB?

a) findOne()
b) findFirst()
c) getOne()
d) fetchOne()

Answer: a) findOne()
Explanation: The findOne() method is used to retrieve the first document that matches the filter criteria.


16. Which operator in MongoDB is used to select documents where the field is not equal to a specified value?

a) $eq
b) $ne
c) $in
d) $gt

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


17. What does the aggregate() function do in MongoDB?

a) Retrieves documents from a collection
b) Aggregates documents to perform advanced computations
c) Updates documents in a collection
d) Deletes documents from a collection

Answer: b) Aggregates documents to perform advanced computations
Explanation: The aggregate() function is used for advanced data processing and computations like grouping, sorting, and filtering.


18. Which MongoDB operation is best used to calculate the average of a numeric field?

a) $average
b) $avg
c) $mean
d) $count

Answer: b) $avg
Explanation: The $avg operator is used in the aggregation pipeline to calculate the average value of a field.


19. What command is used to create an index on the name field of a collection?

a) createIndex({ name: 1 })
b) db.createIndex({ name: 1 })
c) db.collection.createIndex({ name: 1 })
d) createIndex({ field: "name" })

Answer: c) db.collection.createIndex({ name: 1 })
Explanation: The createIndex() method is used to create an index on a specified field, and 1 indicates ascending order.


20. Which method is used to delete multiple documents in MongoDB?

a) remove()
b) deleteAll()
c) deleteMany()
d) clear()

Answer: c) deleteMany()
Explanation: The deleteMany() method is used to delete all documents that match a given filter.


21. What is the default data type used by MongoDB to store dates?

a) String
b) Date
c) ISODate
d) Timestamp

Answer: c) ISODate
Explanation: MongoDB uses the ISODate format to store date and time values.


22. Which operator is used to match a field that is less than a specific value in MongoDB?

a) $lt
b) $gt
c) $eq
d) $lte

Answer: a) $lt
Explanation: The $lt operator is used to match values that are less than the specified value.


23. Which of the following is NOT a valid way to update a document in MongoDB?

a) db.collection.updateOne()
b) db.collection.modifyOne()
c) db.collection.updateMany()
d) db.collection.update()

Answer: b) db.collection.modifyOne()
Explanation: The correct methods are updateOne() and updateMany(). modifyOne() is not a valid method in MongoDB.


24. How would you sort documents in ascending order by the age field in MongoDB?

a) db.collection.find().sort({ age: -1 })
b) db.collection.find().sort({ age: 1 })
c) db.collection.find().sort({ age: 0 })
d) db.collection.find().order({ age: 1 })

Answer: b) db.collection.find().sort({ age: 1 })
Explanation: The sort() method with 1 sorts documents in ascending order based on the age field.


25. How can you ensure that a MongoDB query returns only the specified fields?

a) Use $select operator
b) Use $fields operator
c) Use projection
d) Use get()

Answer: c) Use projection
Explanation: Projection is used to specify which fields should be returned by the query.


26. Which of the following is true about MongoDB's horizontal scaling?

a) MongoDB uses sharding to scale horizontally.
b) MongoDB does not support horizontal scaling.
c) MongoDB uses replication to scale horizontally.
d) MongoDB uses partitioning for horizontal scaling.

Answer: a) MongoDB uses sharding to scale horizontally.
Explanation: MongoDB uses sharding to distribute data across multiple servers, enabling horizontal scaling.


27. Which type of index is most efficient for handling exact matches in MongoDB?

a) Geospatial Index
b) Text Index
c) Hash Index
d) Single Field Index

Answer: d) Single Field Index
Explanation: Single field indexes are efficient for exact matches because they allow fast lookups based on a specific field.


28. Which operator is used to query for documents where a field's value is in an array of values?

a) $in
b) $eq
c) $not
d) $match

Answer: a) $in
Explanation: The $in operator is used to find documents where a field's value matches any value in an array.


29. In MongoDB, what is the purpose of replica sets?

a) Backup storage
b) Provide high availability and redundancy
c) Encrypt data
d) Improve query performance

Answer: b) Provide high availability and redundancy
Explanation: Replica sets in MongoDB provide high availability by replicating data across multiple servers for redundancy and failover.


30. What is the main benefit of using indexes in MongoDB?

a) They increase the size of the database.
b) They help in sorting and filtering queries more efficiently.
c) They slow down insert operations.
d) They improve the readability of data.

Answer: b) They help in sorting and filtering queries more efficiently.
Explanation: Indexes speed up query performance by enabling efficient searching and sorting of data.


31. Which method is used to close a MongoDB connection?

a) db.close()
b) MongoClient.close()
c) db.disconnect()
d) MongoDB.disconnect()

Answer: b) MongoClient.close()
Explanation: The MongoClient.close() method is used to close the connection to the MongoDB server.


32. What does the distinct() method do in MongoDB?

a) Retrieves a single document
b) Retrieves distinct values of a field
c) Deletes documents
d) Updates documents

Answer: b) Retrieves distinct values of a field
Explanation: The distinct() method returns an array of distinct values for a specific field in a collection.


33. Which of the following methods is used to check if a database exists in MongoDB?

a) db.exists()
b) show dbs
c) db.get()
d) db.check()

Answer: b) show dbs
Explanation: The show dbs command lists all databases in the MongoDB instance.


34. How does MongoDB handle data consistency?

a) It uses strong consistency by default.
b) It uses eventual consistency by default.
c) It does not provide consistency.
d) It uses both consistency and availability depending on the configuration.

Answer: b) It uses eventual consistency by default.
Explanation: MongoDB uses eventual consistency by default but can be configured for strong consistency using replica sets.


35. Which of the following is a feature of MongoDB's aggregation framework?

a) It only supports basic queries.
b) It allows performing complex computations and transformations.
c) It requires specific hardware to function.
d) It only works with small datasets.

Answer: b) It allows performing complex computations and transformations.
Explanation: The aggregation framework enables users to perform operations like grouping, filtering, and sorting data with advanced computations.


36. Which MongoDB operator is used to perform a regular expression match on a field?

a) $regex
b) $text
c) $like
d) $search

Answer: a) $regex
Explanation: The $regex operator is used to perform regular expression queries in MongoDB.


37. Which method is used to rename a collection in MongoDB?

a) db.collection.rename()
b) db.collection.renameCollection()
c) db.renameCollection()
d) db.collection.updateName()

Answer: b) db.collection.renameCollection()
Explanation: The renameCollection() method is used to rename a collection in MongoDB.


38. Which type of operation is NOT supported by the MongoDB aggregation pipeline?

a) Grouping
b) Sorting
c) Filtering
d) Full-text search

Answer: d) Full-text search
Explanation: Full-text search is not a part of the MongoDB aggregation pipeline; it requires the use of a text index.


39. Which command is used to remove an index in MongoDB?

a) db.collection.dropIndex()
b) db.collection.deleteIndex()
c) db.collection.removeIndex()
d) db.collection.drop()

Answer: a) db.collection.dropIndex()
Explanation: The dropIndex() method is used to remove an index from a collection.


40. How do you ensure that a field value in MongoDB is unique across a collection?

a) Create a regular index
b) Use the $unique operator
c) Use a unique index
d) Use $addToSet

Answer: c) Use a unique index
Explanation: A unique index ensures that all values for a specific field are unique across the collection.


41. How does MongoDB handle atomic operations?

a) MongoDB supports atomic operations on entire collections only.
b) MongoDB supports atomic operations on individual documents.
c) MongoDB does not support atomic operations.
d) MongoDB supports atomic operations using sharding.

Answer: b) MongoDB supports atomic operations on individual documents.
Explanation: MongoDB supports atomic operations on individual documents, ensuring consistency for each document.


42. Which operator is used to match documents based on the value of an array field?

a) $in
b) $all
c) $elemMatch
d) $arrayFilter

Answer: c) $elemMatch
Explanation: The $elemMatch operator is used to match documents where a field contains an array and the array elements match the query.


43. What does the db.collection.count() method do?

a) Returns the number of documents in the collection
b) Returns the size of the collection in bytes
c) Returns the average size of documents in the collection
d) Deletes documents from the collection

Answer: a) Returns the number of documents in the collection
Explanation: The count() method returns the total number of documents in a collection.


44. What is the purpose of sharding in MongoDB?

a) To increase storage capacity
b) To increase the speed of insert operations
c) To distribute data across multiple servers
d) To back up data to a remote server

Answer: c) To distribute data across multiple servers
Explanation: Sharding is the process of distributing data across multiple servers to support horizontal scaling.


45. Which MongoDB feature allows you to ensure that a write operation is acknowledged by the server?

a) Write concern
b) Read preference
c) Locking
d) Data replication

Answer: a) Write concern
Explanation: Write concern defines the level of acknowledgment requested from MongoDB for write operations.


46. What type of indexing does MongoDB use for geospatial queries?

a) Hash Index
b) GeoJSON Index
c) 2D Geospatial Index
d) Text Index

Answer: c) 2D Geospatial Index
Explanation: MongoDB uses a 2D Geospatial Index to support geospatial queries on data containing longitude and latitude.


47. What is the purpose of the $unset operator in MongoDB?

a) To add a field to a document
b) To remove a field from a document
c) To replace the value of a field
d) To rename a field

Answer: b) To remove a field from a document
Explanation: The $unset operator is used to remove a field from a document.


48. Which command is used to create a new MongoDB user?

a) db.createUser()
b) db.addUser()
c) db.createAccount()
d) db.addAccount()

Answer: a) db.createUser()
Explanation: The db.createUser() method is used to create a new user in MongoDB.


49. How can you remove a document from a MongoDB collection?

a) db.collection.delete()
b) db.collection.drop()
c) db.collection.remove()
d) db.collection.deleteOne()

Answer: d) db.collection.deleteOne()
Explanation: The deleteOne() method is used to remove a single document from a collection.


50. What type of data structure is used to store MongoDB collections?

a) Hash Table
b) B-Tree
c) Binary Tree
d) Hash Map

Answer: b) B-Tree
Explanation: MongoDB uses B-Tree indexing to store and search collections efficiently.

Tags

Post a Comment

0Comments

Post a Comment (0)

About Me