MCQs Of Day 12: Data Modeling in MongoDB
1. What is the key benefit of embedding data in MongoDB?
a) Reduces data redundancy
b) Simplifies complex queries
c) Increases write performance
d) Requires more space for storage
Answer: a) Reduces data redundancy
Explanation: Embedding allows related data to be stored within a single document, reducing redundancy and ensuring that all related data is accessed in a single query.
2. Which of the following MongoDB operations would you use to reference data between documents?
a) $embed
b) $lookup
c) $group
d) $array
Answer: b) $lookup
Explanation: $lookup is used to join documents from different collections, effectively referencing data between collections.
3. In MongoDB, which data model would you choose if you need to model data that changes frequently?
a) Embedded data model
b) Referenced data model
c) Denormalized data model
d) Hybrid data model
Answer: b) Referenced data model
Explanation: If data changes frequently, using references between collections avoids unnecessary updates to embedded data and ensures data consistency.
4. Which of the following is a reason to embed data in MongoDB?
a) To store larger data sets
b) To avoid joins and improve read performance
c) To enforce data consistency
d) To normalize the data
Answer: b) To avoid joins and improve read performance
Explanation: Embedding is often used to model data that is frequently accessed together to improve read performance by avoiding the need for joins.
5. Which of the following is NOT a valid approach in MongoDB data modeling?
a) Embedding related documents within a parent document
b) Referencing other documents by ObjectId
c) Using foreign keys between collections
d) Using $lookup for referencing documents
Answer: c) Using foreign keys between collections
Explanation: MongoDB uses references (ObjectIds) instead of traditional foreign keys in relational databases.
6. When would it be better to use a referenced data model in MongoDB?
a) When data access patterns require frequently joined data
b) When data changes infrequently and is often read together
c) When the data is too large to fit into a single document
d) When the collection is small and doesn't require much optimization
Answer: c) When the data is too large to fit into a single document
Explanation: When data is large or may exceed MongoDB's document size limit (16MB), it's better to store references to the data in different collections.
7. Which MongoDB operator is used to reference data from another collection?
a) $match
b) $lookup
c) $project
d) $group
Answer: b) $lookup
Explanation: $lookup is the MongoDB aggregation operator used to join collections and reference data from another collection.
8. What is the maximum size of a MongoDB document?
a) 16KB
b) 16MB
c) 2GB
d) 1GB
Answer: b) 16MB
Explanation: MongoDB has a 16MB size limit for documents, so it's important to ensure documents do not exceed this size.
9. In MongoDB, which collection should reference user data if the user details change frequently?
a) Orders collection
b) Products collection
c) Users collection
d) Borrowers collection
Answer: c) Users collection
Explanation: If user details change frequently, it's best to store user data in a separate collection and reference it in other collections to minimize redundancy.
10. What is the key difference between embedding and referencing in MongoDB data modeling?
a) Referencing avoids data duplication, while embedding increases data consistency
b) Embedding avoids joins, while referencing avoids data duplication
c) Referencing results in faster reads, while embedding results in faster writes
d) Embedding is used for large data sets, while referencing is used for small data sets
Answer: b) Embedding avoids joins, while referencing avoids data duplication
Explanation: Embedding allows related data to be stored within the same document, while referencing avoids duplication by storing references to data in other documents.
11. In MongoDB, what is the main advantage of using a hybrid data model?
a) It eliminates the need for joins
b) It balances the strengths of both embedding and referencing
c) It ensures data consistency
d) It speeds up write operations
Answer: b) It balances the strengths of both embedding and referencing
Explanation: A hybrid data model uses both embedding and referencing to take advantage of the benefits of both approaches, depending on the use case.
12. When should you avoid embedding data in MongoDB?
a) When the data is small and frequently accessed
b) When the data changes frequently
c) When the data is static and rarely changes
d) When the data is used only for reading
Answer: b) When the data changes frequently
Explanation: Embedding can lead to inefficient updates when data changes frequently, so referencing is a better option in those cases.
13. What is the purpose of using $project in MongoDB aggregation?
a) To group documents based on specific fields
b) To include or exclude fields in the result set
c) To join collections
d) To match documents based on criteria
Answer: b) To include or exclude fields in the result set
Explanation: $project is used in the aggregation pipeline to control the shape of the output document by specifying which fields to include or exclude.
14. Which of the following is an example of a scenario where embedding data in MongoDB would be inefficient?
a) Storing user profiles with posts and comments
b) Storing product details with reviews
c) Storing event details with RSVPs
d) Storing product details with multiple suppliers
Answer: d) Storing product details with multiple suppliers
Explanation: When the number of suppliers is large and changes frequently, embedding this information would be inefficient. It’s better to reference suppliers in a separate collection.
15. What is a potential disadvantage of embedding large arrays in MongoDB documents?
a) Reduces the read performance
b) Makes document updates slower when the array size grows
c) Limits the size of the document
d) Increases the complexity of queries
Answer: b) Makes document updates slower when the array size grows
Explanation: As embedded arrays grow larger, updating documents that contain large arrays becomes slower, especially if the entire document needs to be modified.
16. Which of the following is NOT a MongoDB collection type?
a) Embedded Collection
b) Single Collection
c) Subcollection
d) Referenced Collection
Answer: c) Subcollection
Explanation: MongoDB doesn’t support subcollections. It uses embedded documents or references to relate data within a single collection.
17. What is the purpose of schema validation in MongoDB?
a) To enforce data integrity and prevent incorrect data types
b) To define the relationships between collections
c) To optimize data storage
d) To prevent document size from exceeding 16MB
Answer: a) To enforce data integrity and prevent incorrect data types
Explanation: Schema validation in MongoDB is used to enforce rules for the data being inserted or updated in the database, ensuring that data follows certain formats or types.
18. In MongoDB, which of the following would be the best approach for modeling data that has many-to-many relationships?
a) Embedding data
b) Referencing data
c) Creating a junction collection
d) Storing data in arrays
Answer: c) Creating a junction collection
Explanation: In many-to-many relationships, a junction collection that stores references to both related collections is the most efficient way to model the data.
19. When using a referenced data model, which of the following is NOT a common operation?
a) Performing a $lookup to join collections
b) Using ObjectId to reference documents
c) Storing large documents within a single collection
d) Querying for referenced data in multiple collections
Answer: c) Storing large documents within a single collection
Explanation: Storing large documents is more typical in embedded data models. Referencing models typically store smaller references and use joins.
20. Which of the following is a feature of MongoDB’s document model?
a) Fixed schema structure
b) Use of foreign keys for referencing
c) Flexibility to store documents with different fields
d) Fixed document size limit of 1GB
Answer: c) Flexibility to store documents with different fields
Explanation: MongoDB's schema-less nature allows documents in the same collection to have different fields.
21. Which MongoDB operation would you use to add new fields or modify the values of existing fields in a document?
a) $project
b) $addFields
c) $lookup
d) $match
Answer: b) $addFields
Explanation: $addFields is used to add new fields or modify existing fields in MongoDB documents during aggregation.
22. What does MongoDB use to handle complex relationships between collections?
a) Embedding
b) $group
c) $lookup
d) Aggregation pipeline
Answer: c) $lookup
Explanation: $lookup allows you to perform a join between two collections in MongoDB, handling complex relationships by referencing data from another collection.
23. When would you prefer a referenced model over an embedded model?
a) When you need to store large, frequently changing data
b) When the data is static and frequently accessed together
c) When the data changes rarely
d) When the data is stored in one-to-one relationships
Answer: a) When you need to store large, frequently changing data
Explanation: Referenced models are ideal when the data is large or changes frequently, as it prevents the need to update large embedded documents.
24. Which of the following is true about MongoDB’s data model?
a) All data in MongoDB must be stored in a relational format
b) MongoDB does not allow storing large arrays
c) MongoDB documents can store nested arrays and objects
d) MongoDB requires all documents in a collection to have the same fields
Answer: c) MongoDB documents can store nested arrays and objects
Explanation: MongoDB allows storing complex, nested data structures such as arrays and objects within a document.
25. How can you enforce data integrity in MongoDB without using relational constraints?
a) Use data validation rules in collections
b) Use foreign keys
c) Implement triggers in MongoDB
d) Use multiple collections for each data type
Answer: a) Use data validation rules in collections
Explanation: MongoDB allows you to use schema validation to enforce data integrity without foreign keys, ensuring that documents follow a set structure or rules.
26. What is the main purpose of using the $match operator in MongoDB?
a) To join two collections
b) To filter documents based on specific criteria
c) To add new fields to documents
d) To modify documents
Answer: b) To filter documents based on specific criteria
Explanation: $match is used in the aggregation pipeline to filter documents based on specified conditions.
27. What is the maximum depth for nesting documents in MongoDB?
a) 10 levels
b) 100 levels
c) No limit
d) 2 levels
Answer: c) No limit
Explanation: MongoDB allows nesting documents to any depth, but practical considerations like performance and document size limit may impose constraints.
28. What is the benefit of using MongoDB’s aggregation pipeline?
a) It simplifies query writing
b) It allows complex data transformations
c) It handles data modeling
d) It reduces the database size
Answer: b) It allows complex data transformations
Explanation: The aggregation pipeline in MongoDB allows for powerful data transformations like filtering, grouping, sorting, and joining.
29. In MongoDB, what is the purpose of using the $unwind operator?
a) To combine multiple documents into one
b) To break an array into multiple documents
c) To sort documents
d) To create indexes
Answer: b) To break an array into multiple documents
Explanation: $unwind is used to deconstruct an array field into separate documents, each containing a single element from the array.
30. Which of the following is a scenario where embedding data is a better choice in MongoDB?
a) When the data needs to be referenced in many places
b) When the data is updated frequently
c) When the data is frequently accessed together
d) When the data is large and needs to be stored separately
Answer: c) When the data is frequently accessed together
Explanation: Embedding data is ideal when related data is frequently accessed together as it reduces the need for joins and improves read performance.
I have provided the first 30 MCQs for MongoDB data modeling. Would you like me to continue with the remaining 20 questions in the same format?
4o mini
You said:
rest 20
ChatGPT said:
ChatGPT
Here are the remaining 20 MCQs for the concept of "Indexes in MongoDB," with explanations:
MCQs 31–50
31. Which method is used to view all indexes on a MongoDB collection?
- A) db.collection.listIndexes()
- B) db.collection.showIndexes()
- C) db.collection.getIndexes()
- D) db.collection.viewIndexes()
Answer: C
Explanation: The getIndexes() method lists all the indexes in a collection.
32. What is the default index created automatically on a MongoDB collection?
- A) Compound Index
- B) Unique Index
- C) Single-field Index
- D) _id Index
Answer: D
Explanation: MongoDB automatically creates an _id index for every collection.
33. How many single-field indexes can be created on a single collection?
- A) One
- B) Ten
- C) Unlimited
- D) Depends on storage capacity
Answer: C
Explanation: There is no strict limit to the number of single-field indexes, though practical limits are based on performance and storage.
34. Which MongoDB operator forces the query to use a specific index?
- A) $hint
- B) $useIndex
- C) $forceIndex
- D) $queryPlan
Answer: A
Explanation: The $hint operator explicitly tells MongoDB which index to use.
35. Which of the following is NOT true about indexes in MongoDB?
- A) Indexes improve query performance.
- B) Indexes increase the write operation time.
- C) Indexes decrease storage requirements.
- D) Compound indexes can be created for multiple fields.
Answer: C
Explanation: Indexes require additional storage, so they increase storage requirements.
36. Which of the following can be used to create a unique index in MongoDB?
- A) { unique: true }
- B) { unique: false }
- C) { unique: "yes" }
- D) { uniqueIndex: true }
Answer: A
Explanation: The option { unique: true } creates an index that ensures all values are unique.
37. What is the main disadvantage of creating too many indexes?
- A) Query performance decreases.
- B) Write performance decreases.
- C) Storage size decreases.
- D) Data integrity is affected.
Answer: B
Explanation: Too many indexes can slow down write operations because all indexes need to be updated when data changes.
38. Which type of index is best for queries on fields with high cardinality (many unique values)?
- A) Hashed Index
- B) Single-field Index
- C) Compound Index
- D) Wildcard Index
Answer: B
Explanation: Single-field indexes perform well for high-cardinality fields like unique IDs.
39. Which index type is designed for geographical data in MongoDB?
- A) Geospatial Index
- B) Geo Index
- C) GeoHash Index
- D) Location Index
Answer: A
Explanation: Geospatial indexes (2d or 2dsphere) are used for querying geographical data.
40. What happens if you attempt to insert duplicate values in a field with a unique index?
- A) The operation succeeds.
- B) The operation fails with an error.
- C) The value is ignored.
- D) A new document is created with a null value.
Answer: B
Explanation: Unique indexes enforce uniqueness, so duplicate values are rejected.
41. Which method is used to remove an index in MongoDB?
- A) db.collection.dropIndex()
- B) db.collection.removeIndex()
- C) db.collection.deleteIndex()
- D) db.collection.clearIndex()
Answer: A
Explanation: The dropIndex() method removes a specific index from a collection.
42. Which statement is true about compound indexes?
- A) Compound indexes are created on a single field.
- B) The order of fields in a compound index does not matter.
- C) Compound indexes are created on multiple fields.
- D) Compound indexes cannot be created on text fields.
Answer: C
Explanation: Compound indexes involve multiple fields and improve performance for queries involving those fields.
43. Can an index be created on a nested field in a document?
- A) Yes, using dot notation.
- B) No, only top-level fields are allowed.
- C) Yes, but only for arrays.
- D) No, nested fields are automatically indexed.
Answer: A
Explanation: Nested fields can be indexed using dot notation, like field.subfield.
44. What is a sparse index in MongoDB?
- A) An index that ignores null and missing values.
- B) An index that compresses data.
- C) An index on array fields.
- D) A temporary index.
Answer: A
Explanation: Sparse indexes only include documents where the indexed field has a value.
45. Which of the following queries can benefit most from a hashed index?
- A) Range queries
- B) Equality queries
- C) Text search queries
- D) Geospatial queries
Answer: B
Explanation: Hashed indexes are optimized for equality queries and not for range queries.
46. Which type of index is used for text search in MongoDB?
- A) Text Index
- B) String Index
- C) Hashed Index
- D) Alphabet Index
Answer: A
Explanation: Text indexes allow for full-text search of string fields.
47. What is the correct syntax for creating an index in MongoDB?
- A) db.collection.createIndex({ field: 1 })
- B) db.collection.addIndex({ field: 1 })
- C) db.collection.newIndex({ field: 1 })
- D) db.collection.setIndex({ field: 1 })
Answer: A
Explanation: The createIndex() method is used to create indexes in MongoDB.
48. What happens if an index is created with the same key and options as an existing index?
- A) MongoDB creates a duplicate index.
- B) The existing index is updated.
- C) MongoDB skips index creation.
- D) An error is thrown.
Answer: C
Explanation: MongoDB does not create duplicate indexes with identical specifications.
49. Which of the following fields cannot be indexed?
- A) Array fields
- B) Text fields
- C) Fields with null values
- D) Fields larger than the index size limit
Answer: D
Explanation: MongoDB cannot index fields that exceed the maximum index size.
50. What is the default index order for fields in MongoDB?
- A) Ascending
- B) Descending
- C) Random
- D) Custom
Answer: A
Explanation: The default index order is ascending (1).
