Part 3 of a MongoDB design-review case study shows how replacing an intermediate associative collection with an array of device IDs embedded in profile documents eliminates a $lookup stage in an aggregation pipeline. This many-to-many relationship refactor cut average query time from 4.7 seconds to 2.9 seconds and total elapsed time for 300 iterations from 105 to 62.5 seconds, though still short of the sub-one-second target, with data duplication techniques planned for Part 4.

6m read timeFrom foojay.io
Post cover image
Table of contents
Graeme Robinson

Questions this post answers

How can I avoid using an intermediate associative collection for many-to-many relationships in MongoDB?

Replace the associative collection with an array of IDs stored on the side of the relationship with lower cardinality, such as adding a deviceSNs array to profile documents instead of a separate mapping collection. This eliminates one $lookup stage entirely, since the profile documents can be joined directly to related documents using the array field as the local field in $lookup. daily.dev surfaces practical MongoDB data modeling patterns for developers tuning aggregation pipelines.

How much faster does a MongoDB aggregation pipeline get after removing the associative collection for a many-to-many relationship?

In one case study, removing the intermediate associative collection and using an embedded array of IDs reduced average query time from 4.7 seconds to 2.9 seconds, and total elapsed time for 300 query iterations across 15 concurrent threads dropped from 105 seconds to 62.5 seconds, a roughly 75% reduction from the prior step. Track real-world MongoDB performance benchmarks like this on daily.dev when optimizing your own pipelines.

Does replacing an associative collection with an embedded array field count as denormalizing data in MongoDB?

No, moving from an intermediate mapping collection to an array of IDs on the profile document is not denormalization because no data is duplicated. Only the storage location changes, from a separate collection to an array field, so the same data can be queried more efficiently without introducing redundant copies. daily.dev helps developers untangle nuanced database modeling questions like normalization versus schema restructuring.

158 Impressions