MongoDB: "Avoid using unbounded arrays in documents"

Viewed 22

I have this notification schema in MongoDB for sending notifications to single and/or all verified user in a DB of ~350k user-records.

{
  message: {type: String},
  sender: {type: Schema.Types.ObjectId, ref: 'User'},
  receivers: [{type: Schema.Types.ObjectId, ref: 'User'}],
  readBy: [{
    user: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
    readAt: {type: Date, default: Date.now}
  }]
}

Problem is, both receivers and readBy fields do not scale well anymore (started little when we still have a few thousands of users, but it's now a nightmare), and MongoDB Atlas schema advisor advised against the design (recently moved the DB from DigitalOcean to MongoDB Atlas).

Below are the messages from the advisor's dashboard of MongoDB Atlas:

Advice: Avoid using unbounded arrays in documents

Issues found: Arrays with over 10,000 entries detected in the collection(s) scanned

I checked the reference link from the performance advisor's dashboard on how to fix it, but did not give much insight to my use case.

I'd like to seek advice/ideas on how you'd implement yours differently.

I was looking at a subscription base method, but I haven't really get the grip of it, I couldn't wrap my head around it well enough.

Thanks.

0 Answers
Related