214k views
1 vote
A MongoDB schema is being designed to store details of orders and the products contained in those orders. A many-to-many relationship exists between orders and products, because an order can be for many products and a product can appear in many orders. The design options being considered are given below. State which one you believe to be correct for a MongoDB design.

Options:
a. Store an array containing all the product IDs within the order document for that order, and store an array of order IDs in the product document containing all the orders requesting that product.
b. Create a 3rd collection that contains documents comprising two fields corresponding to an order_id and the ID of a product appearing in that order, thus resolving the many-to-many relationship.
c. Use a relational database instead of MongoDB to handle the many-to-many relationship.
d. Create a separate collection for each order and product combination to simplify the structure.

User Treaschf
by
7.3k points

1 Answer

4 votes

Final answer:

Option b) is the correct design for a MongoDB schema to handle a many-to-many relationship between orders and products.

Step-by-step explanation:

Option b) is the correct design for a MongoDB schema to handle a many-to-many relationship between orders and products. Creating a 3rd collection that contains documents with fields corresponding to an order_id and the ID of a product appearing in that order allows for efficient querying and retrieval of data.

The other options have drawbacks:

  1. Option a) stores all the product IDs within the order document, which can lead to inefficient querying and difficulty in retrieving specific information.
  2. Option c) suggests using a relational database instead of MongoDB, which would involve changing the entire database system, creating more complexity and potential migration difficulties.
  3. Option d) proposes creating a separate collection for each order and product combination, which would lead to a large number of collections and complicated querying.

User Adrian Ivasku
by
7.9k points