---
title: "Aggregation Optimization in MongoDB: Optimizing Many-to-Many Relationships (Part 3)"
url: https://daily.dev/posts/aggregation-optimization-in-mongodb-optimizing-many-to-many-relationships-part-3--uiixfwluy
source_url: https://foojay.io/today/aggregation-optimization-in-mongodb-optimizing-many-to-many-relationships-part-3
type: article
source: "Foojay.io"
published: 2026-08-21T16:03:57.694Z
updated: 2026-08-21T16:04:37.245Z
tags: ["mongodb", "nosql"]
reading_time: 6
upvotes: 0
comments: 0
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Aggregation Optimization in MongoDB: Optimizing Many-to-Many Relationships (Part 3)

**[Foojay.io](https://daily.dev/sources/foojayio)** · 6 min read · 0 upvotes · 0 comments

## Summary

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.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://foojay.io/today/aggregation-optimization-in-mongodb-optimizing-many-to-many-relationships-part-3>

## 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._

## Similar posts on daily.dev

- [Aggregation Optimization in MongoDB: A Case Study From the Field \(Part 1\)](https://daily.dev/posts/aggregation-optimization-in-mongodb-a-case-study-from-the-field-part-1--gzqybwhpz) · Foojay.io · 0 upvotes · 0 comments
- [Aggregation Optimization in MongoDB: Unnecessary Unwinds \(Part 2\)](https://daily.dev/posts/aggregation-optimization-in-mongodb-unnecessary-unwinds-part-2--cfmv8gh1z) · Foojay.io · 0 upvotes · 0 comments

---

Tags: [#mongodb](https://daily.dev/tags/mongodb), [#nosql](https://daily.dev/tags/nosql)

[View this post on daily.dev](https://daily.dev/posts/aggregation-optimization-in-mongodb-optimizing-many-to-many-relationships-part-3--uiixfwluy)
