1

I am mapping relations in mongoid, I want to know the best practices for mongoid.

Models

  1. Customer
  2. Service
  3. Supplier

there are four different services (S1, S2, S3, S4), each service provided by the specific supplier. How can I map these models or build the relations among them?

Thanks in advance.

bilal ahmad
  • 206
  • 3
  • 9

2 Answers2

0

You can either of these approaches, both supported by Mongoid:

  • Referenced 1-N (aka has_many)
    In this model, each Service would store the _id of the Supplier that provides it, similar to a typical RDBMS.

  • Embedded 1-N (aka embeds_many)
    In this model, each Supplier document would just store an internal data structure representing the list of Service(s) it provides.

As with most schema decisions in MongoDB, the approach you choose should largely be based on your use cases, and what kinds of queries you anticipate needing in your application. This question provides a good overview when to embed vs. reference in MongoDB:

MongoDB relationships: embed or reference?

See the Relations page of the Mongoid docs for more details about relationships in Mongoid.

Community
  • 1
  • 1
Stuart M
  • 11,458
  • 6
  • 45
  • 59
0

Just my 2 cents:

Customer N:1 Supplier

S1 < Supplier

S2 < Supplier

S3 < Supplier

S4 < Supplier

Hlex
  • 833
  • 8
  • 16