Watch keynotes and sessions from MongoDB.live, our virtual developer conference.

Stock the data or calculate 'on the fly', how to choose?

Hello everyone !

In our project we face always the same question, shall we just calculate the derived data ‘on the fly’ or shall we stock and maintain derived data (with callbacks and scripts)?

Let’s say I have the collections “Projects” and “Users”. I want to know all my projects so I can
1 Create a field userId in Projects and look for all the projects with this userId (having indexed) every time.
Or
2 Create an array myProjectIds in Users plus the userId in Projects. myProjects should be maintained with callbacks and scripts turning each x time.
This time the first option seems easier and enough

But what if we want to look for all the projects I belong to in any way to a project and I have too many fields to query in each document (let’s say I have an array teamMembers with and object in each)?

Of course we can measure the time to answer of the differente queries, but is there any rule to know in advance if it’s worth it to take the second approach?

Thanks for you answers!

1 Like