MongoDB MCQs – Relationships in MongoDB

Relationships in MongoDB are crucial for modeling connections between data, similar to relational databases. By understanding One-to-One, One-to-Many, and Many-to-Many relationships, developers can design efficient schemas based on the application’s requirements. These MCQs help candidates learn the differences between embedding and referencing, optimize queries using $lookup, and handle real-world situations.

1.) What is the purpose of relationships in MongoDB?

A) To establish connections between data in different collections
B) To index fields automatically
C) To store data in a single collection
D) To enforce strict schemas

Answer: Option A

Explanation: Relationships in MongoDB connect data across different collections, similar to table relationships in relational databases.

2.) Which of the following is a type of relationship in MongoDB?

A) One-to-One
B) One-to-Many
C) Many-to-Many
D) All of the above

Answer: Option D

Explanation: MongoDB supports all types of relationships: One-to-One, One-to-Many, and Many-to-Many, using references or embedded documents.

3.) How are One-to-One relationships typically modeled in MongoDB?

A) Using arrays
B) Using embedded documents
C) Using references with $lookup
D) Both B and C

Answer: Option D

Explanation: One-to-One relationships can be modeled using embedded documents for simplicity or references when flexibility is required.

4.) What is the preferred approach for a One-to-Many relationship when the “many” side has few documents?

A) Embedding documents
B) Using $lookup
C) Creating a separate collection
D) Indexing the field

Answer: Option A

Explanation: When the “many” side has a limited number of documents, embedding is preferred for simplicity and performance.

5.) What does the $lookup stage in aggregation do?

A) Embeds documents in a collection
B) Creates indexes for relationships
C) Joins data from two collections
D) Deletes related documents

Answer: Option C

Explanation: The $lookup stage performs a left outer join to fetch related documents from another collection.

6.) Which method fetches related documents using manual references?

A) $lookup
B) db.collection.find({ _id: referenceId })
C) $merge
D) db.collection.join(referenceId)

Answer: Option B

Explanation: Manual references require querying the related collection using the stored reference ID.

7.) How are Many-to-Many relationships typically implemented in MongoDB?

A) Using arrays of references
B) Using embedded documents
C) Using a junction (or bridge) collection
D) Using $group

Answer: Option C

Explanation: A junction collection is used to model Many-to-Many relationships by storing references to documents from both collections.

8.) What is an advantage of using embedded documents over references?

A) Data normalization
B) Faster read performance
C) Reduced document size
D) Increased flexibility

Answer: Option B

Explanation: Embedded documents improve read performance because all related data is stored in a single document.

9.) Which is a drawback of embedding data for relationships?

A) Data duplication
B) Increased write complexity
C) Requires additional queries
D) Inefficient indexing

Answer: Option A

Explanation: Embedding may lead to data duplication, making updates more complex when the same data exists in multiple documents.

10.) In a normalized data model, where are related data stored?

A) In the same document
B) In a separate collection
C) As an index
D) In an array field

Answer: Option B

Explanation: In normalized models, related data is stored in separate collections, with references linking them.

Leave a Reply

Your email address will not be published. Required fields are marked *