简体   繁体   中英

Meteor CRUD many-to-many relationships MongoDB

I have 3 collections interconnected through many-to-many relationships. Therefore, I have 2 concerns:

  1. Should I have 2 arrays with 2 ids in each of the 3 collections, or one join collection with 3 ids?
  2. How to perform reads, inserts, updates and deletes, so everything is in sync and integrity is ensured?

For most scenarios, I'd probably have the referencing IDs on each of the objects, like this:

{
  "_id": "123",
  "firstReferenceCollectionId": "abc",
  "secondReferenceCollectionId": "def"
}

If your application is going to have massive scale, I'd probably further denormalize the data based on how it's actually being used.

To answer your second question, you really shouldn't need to worry about that with the above approach, since the internal id of the reference won't change when other properties on those objects change. If you go the route of additional denormalization, use meteor add matb33:collection-hooks to sync up data on upserts. Here's the documentation link: https://github.com/matb33/meteor-collection-hooks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM