简体   繁体   中英

MongoDB - store strategy

Coming from relational databases like your help on how to model my data. What is the best efficient way?

I'm working on a very simple microservice to manage organizations. each organization has one field: name and list of users id's included in that organizations.

I can see two ways of modeling that data:

1. separate documents:

organizations collection:

{
   id: '88c4e67a-c4ba-11e9-aa8c-2a2ae2dbcce4',
   name: 'some organization'
}

users collection:

{ 
   userId: '7e27ff68-c4ba-11e9-aa8c-2a2ae2dbcce4', 
   organizationId: '88c4e67a-c4ba-11e9-aa8c-2a2ae2dbcce4'
}

2. or as nested documents:

organizations collection:

{
   id: '88c4e67a-c4ba-11e9-aa8c-2a2ae2dbcce4',
   name: 'some organization,
   users: [
      '7e27ff68-c4ba-11e9-aa8c-2a2ae2dbcce4',
   ]
}

from your experience, what the pros & cons of each strategy? what your preferred way? anything to consider in each strategy? is there is another better way to model this data in mongodb?

EDIT: The types of queries I'll need is to:

  1. fetch all organizations data (no necessarily need for the users id's)
  2. fetch organization data (including it's users) by organization id
  3. fetch organization by user id.

Thanks in advance

Given the high-level use case, I would prefer the de-normalized model (nested) which is the advantage you've in mongoDB and we must utilize it to get more out of mongo. The 'option 2' that you've mentioned is not nested document, you're just maintaining the field as array

In nested model, the data will look like:

{
   id: '88c4e67a-c4ba-11e9-aa8c-2a2ae2dbcce4',
   name: 'some organization,
   users: {
      user1: 'abc',
      user2: 'def',
      user3: 'xyz',
   }
}

Thank you!

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