Optimized Schema with Array

Hey, I couldn’t make a schema for my project due to some fields inside the schema.
Basically, the schema has only two fields which are “group_id” and “members”. The problem is members field can has 10,000 elements inside but not every field. The document isn’t exceed the limit of 16MB but I am worried about the performance.

What do you think about it? Should I keep continue with current schema?

group_id: "group_1"
members:
  0:
    user_id: "test_user0"
    role: "ADMIN"
    tag: "tag_1"
  1:
    user_id: "test_user1"
    role: "MOD"
    tag: "tag_1"
1 Like

Hello @Duck, welcome to the MongoDB Community forum.

…worried about the performance.

The question I see is why are you worried about performance? What is it you are going to (or trying) do with the data in the members array.

You can insert new elements, update or delete elements in the array. And query too. Array fields can have indexes - and these are called Multikey Indexes. The queries on array fields can benefit, in terms of performance, from Multikey indexes. You can study the query performance by generating query plans.

As such your current model might work fine as it is. The kind of queries (the important ones and includes CRUD) you are going to have in your application can determine the design of the data model. So, what are these queries?

2 Likes

It often uses “get”, “$push” and “$pull” queries.

I’ve indexes for in array objects but I am worried about size of the array how it is going to affects to queries.

If you know, the size of array is going to be finite (e.g., it can be between 1 and 10,000 only - and never more than that) and the size of the document is within limits - I think it will workout fine.

The find and update (push and pull operations on the array) queries use the index when the query condition has the indexed field(s).

…I am worried about…

I suggest you create some test data and run the important queries. Use the explain() method on the queries and study the plan output statistics.

Then, there is always the option of re-consider the present design.

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.