Although Prasad_Saya’s solution works but it might have issues if the embedded data grows.
What I would suggest is to use the Subset pattern where you store only a subset of the documents as embeds and the rest in its own collection, let me explain:
Imagine we have a Posts collection and a Likes collection. There can be millions of posts and millions of likes - In our Posts collection, we’ll keep a subset of the likes — let’s say the most recent 100 likes — to keep things snappy. Meanwhile, all the likes, including the older ones, will reside comfortably in our Likes collection.
Here’s a glimpse of how our documents might look:
posts:
// Posts Collection
{
_id: <post_id>,
content: <string>,
author: <user_id>,
likes: [
// Keep only the most recent likes here (Latest 100 likes)
{ user_id: <user_id>, timestamp: <date> },
{ user_id: <user_id>, timestamp: <date> },
...
]
}
likes:
// Likes Collection
{
_id: <like_id>,
post_id: <post_id>,
user_id: <user_id>,
timestamp: <date>,
// Store all likes here, including the older ones
}
We keep the most frequently accessed likes directly within the post documents, reducing the working set and improving performance. Meanwhile, the older likes peacefully reside in their own cozy corner, ready to be fetched whenever needed.
Here is an article I have written on medium explaining this: How to model data for many to many relationship in MongoDB | by Ashique Desai | Apr, 2024 | Medium
Additionally a link to MongoDB blog explaining the subset pattern: