MongoDB MCQs – Relationships in MongoDB

11.) Which aggregation stage can be used to handle Many-to-Many relationships?

A) $match
B) $project
C) $group
D) $lookup

Answer: Option D

Explanation: $lookup is commonly used to join data from related collections in Many-to-Many relationships.

12.) Which command can be used to optimize One-to-Many queries involving references?

A) $match
B) $lookup
C) $sort
D) $facet

Answer: Option B

Explanation: $lookup optimizes One-to-Many queries by performing a join between the collections.

13.) Which $lookup parameter defines the collection to join?

A) from
B) as
C) localField
D) foreignField

Answer: Option A

Explanation: The from parameter specifies the foreign collection to join in the $lookup stage.

14.) In which situation is embedding a poor choice for modeling relationships?

A) When related data rarely changes
B) When there is a One-to-One relationship
C) When related data grows without limits
D) When queries require frequent joins

Answer: Option C

Explanation: Embedding is inefficient for relationships where related data can grow indefinitely, as it leads to oversized documents.

15.) How do you specify the output field for a $lookup operation?

A) outputField
B) as
C) result
D) alias

Answer: Option B

Explanation: The as parameter specifies the name of the field where the results of the $lookup are stored.

16.) Which relationship type is best handled using arrays of embedded documents?

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

Answer: Option B

Explanation: Arrays of embedded documents work well for One-to-Many relationships when the “many” side is small.

17.) What is a potential drawback of using $lookup?

A) Reduced query flexibility
B) Limited support for text fields
C) Requires schema enforcement
D) Increased query execution time

Answer: Option D

Explanation: $lookup can increase query execution time, especially with large collections, as it performs a join operation.

18.) What is the main advantage of referencing over embedding?

A) Simpler queries
B) Faster read performance
C) Reduced document size
D) No need for additional collections

Answer: Option C

Explanation: Referencing keeps the document size small by storing only the reference ID, rather than embedding all data.

19.) Which MongoDB feature allows real-time data updates across related documents?

A) $lookup
B) Change Streams
C) Triggers
D) Aggregation

Answer: Option B

Explanation: Change Streams allow real-time updates and monitoring of data changes in collections.

20.) What is a key limitation of Many-to-Many relationships modeled with arrays?

A) Cannot use indexes
B) Harder to query
C) Data duplication
D) Array size limit

Answer: Option D

Explanation: Arrays have size limitations, making them unsuitable for large Many-to-Many relationships.

Leave a Reply

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