Need a piece of advice for storing a large number of references between Mongo entities that are frequently updated.

Some background:

  • 1 event can have 5000 logs.
  • 1 event can have 10 assets associated (assignment object).
  • Logs are created per asset per event.
  • Query pattern: find all logs by assignment (eventId and assetId).

Entities with their relations:

EVENT <-1--------------N-> LOG   

Mongo collections and documents:

event: { id, name, ... }
asset: { id, name, ... }
log: { id, eventId, metadata, ... }
assignment: { id, assetId, eventId, metadata, ... }

Here comes the question:

  • What is the best way to store associations between logs and assignments (events and assets)? Note that those associations are updated frequently and concurrently.

Approach 1

Store logIds in assignment entity:

assignment: { id, assetId, eventId, logIds, ... }

Approach 2

Create a separate entity for holding a single reference between log and assignment:

assignmentLog: { assignmentId, logId }

Another approach ?

Any advice will be appreciated. Thanks.

  • I am trying to understand your question, logs and assignments are directly related to events with 1 to N mapping, Assets are directly related to assignments with 1 to N mapping. {"eventId":1,"logs":[{"logId":1},{"logId":2},{"logId":3},{"logId":4},{"logId":5}],"assignments":[{"assigmentId":1,"assets":[{"assetId":11},{"assetId":12},{"assetId":13},{"assetId":14}]},{"assigmentId":2,"assets":[{"assetId":21},{"assetId":22},{"assetId":23},{"assetId":24}]},{"assigmentId":3,"assets":[{"assetId":31},{"assetId":32},{"assetId":33},{"assetId":34}]}]} Is my understanding correct? – Mani Mar 23 '19 at 17:16
  • Correct. But there is one more relation: asset<-1—N->log. And I am thinking what is the best way to store those relations. Note that asset logs is a subset of event logs. For example: event1 has [log1, log2, log3] and event1 is assigned to [asset1, asset2]. At the same time event1-asset1 can have [log1, log2] and event1-asset2 can have just [log3]. Hope it is more clear now :) – Bogdan Kobylynskyi Mar 23 '19 at 21:12
  • 1
    Please have a look at these bast practices mongodb.com/blog/post/… mongodb.com/blog/post/… mongodb.com/blog/post/… – Mani Mar 23 '19 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.