DEV Community

Apurv Upadhyay
Apurv Upadhyay

Posted on

๐Ÿ’ก๐—˜๐˜ƒ๐—ฒ๐—ฟ ๐—ณ๐—ผ๐˜‚๐—ป๐—ฑ ๐˜†๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐—น๐—ณ ๐—ฑ๐—ฒ๐—ฏ๐—ฎ๐˜๐—ถ๐—ป๐—ด ๐—ฏ๐—ฒ๐˜๐˜„๐—ฒ๐—ฒ๐—ป ๐˜‚๐˜€๐—ถ๐—ป๐—ด ๐—ฎ๐—ฟ๐—ฟ๐—ฎ๐˜†๐˜€ ๐—ผ๐—ฟ ๐˜€๐—ฒ๐—ฝ๐—ฎ๐—ฟ๐—ฎ๐˜๐—ฒ ๐—ฑ๐—ผ๐—ฐ๐˜‚๐—บ๐—ฒ๐—ป๐˜๐˜€ ๐—ถ๐—ป ๐— ๐—ผ๐—ป๐—ด๐—ผ๐——๐—•?

Today, I faced this challenge while structuring click events performed on a screen. The estimation shows that a single location could accumulate over 50K records or more.

Image description

So, the decision comes down to:
โ€ข Should we use an ๐—ฎ๐—ฟ๐—ฟ๐—ฎ๐˜† ๐˜€๐˜๐—ฟ๐˜‚๐—ฐ๐˜๐˜‚๐—ฟ๐—ฒ where ๐—ฐ๐—ฒ๐—ป๐˜๐—ฒ๐—ฟ๐—œ๐—ฑ and ๐—ฎ๐—ฝ๐—ฝ๐—ผ๐—ถ๐—ป๐˜๐—บ๐—ฒ๐—ป๐˜๐——๐—ฎ๐˜๐—ฒ keep one dayโ€™s record in a single document?
โ€ข Or should we opt for ๐˜€๐—ฒ๐—ฝ๐—ฎ๐—ฟ๐—ฎ๐˜๐—ฒ ๐—ฑ๐—ผ๐—ฐ๐˜‚๐—บ๐—ฒ๐—ป๐˜๐˜€ for each transaction?

Letโ€™s dive into what works best and why! ๐Ÿ‘‡

๐—ž๐—ฒ๐˜† ๐—–๐—ผ๐—ป๐˜€๐—ถ๐—ฑ๐—ฒ๐—ฟ๐—ฎ๐˜๐—ถ๐—ผ๐—ป๐˜€:

  1. ๐—ฆ๐—ฐ๐—ฎ๐—น๐—ฎ๐—ฏ๐—ถ๐—น๐—ถ๐˜๐˜† ๐Ÿš€ โ€ข ๐—”๐—ฟ๐—ฟ๐—ฎ๐˜†๐˜€: Risk of exceeding MongoDBโ€™s 16 MB document size limit, making it unsuitable for large datasets. โ€ข ๐—ฆ๐—ฒ๐—ฝ๐—ฎ๐—ฟ๐—ฎ๐˜๐—ฒ ๐——๐—ผ๐—ฐ๐˜‚๐—บ๐—ฒ๐—ป๐˜๐˜€: Supports sharding, allowing data distribution across servers as the dataset grows, ensuring scalability.
  2. ๐—ค๐˜‚๐—ฒ๐—ฟ๐˜† ๐—ฃ๐—ฒ๐—ฟ๐—ณ๐—ผ๐—ฟ๐—บ๐—ฎ๐—ป๐—ฐ๐—ฒ โšก โ€ข ๐—”๐—ฟ๐—ฟ๐—ฎ๐˜†๐˜€: MongoDB must scan entire arrays, which becomes inefficient as arrays grow. โ€ข ๐—ฆ๐—ฒ๐—ฝ๐—ฎ๐—ฟ๐—ฎ๐˜๐—ฒ ๐——๐—ผ๐—ฐ๐˜‚๐—บ๐—ฒ๐—ป๐˜๐˜€: Efficient querying with indexed fields like centerId and appointmentDate, enabling fast lookups and aggregations.
  3. ๐—™๐—น๐—ฒ๐˜…๐—ถ๐—ฏ๐—ถ๐—น๐—ถ๐˜๐˜† ๐—ฎ๐—ป๐—ฑ ๐— ๐—ฎ๐—ถ๐—ป๐˜๐—ฒ๐—ป๐—ฎ๐—ป๐—ฐ๐—ฒ ๐Ÿงฉ โ€ข ๐—”๐—ฟ๐—ฟ๐—ฎ๐˜†๐˜€: Modifying or deleting specific elements in large arrays can be cumbersome. โ€ข ๐—ฆ๐—ฒ๐—ฝ๐—ฎ๐—ฟ๐—ฎ๐˜๐—ฒ ๐——๐—ผ๐—ฐ๐˜‚๐—บ๐—ฒ๐—ป๐˜๐˜€: Easier to update or delete individual records, maintaining data integrity and consistency with minimal effort.

๐—ง๐—ต๐—ฒ ๐—•๐—ฒ๐˜€๐˜ ๐—”๐—ฝ๐—ฝ๐—ฟ๐—ผ๐—ฎ๐—ฐ๐—ต:
๐—ฆ๐—ฒ๐—ฝ๐—ฎ๐—ฟ๐—ฎ๐˜๐—ฒ ๐——๐—ผ๐—ฐ๐˜‚๐—บ๐—ฒ๐—ป๐˜๐˜€: โœ…:
โ€ข ๐—ช๐—ต๐˜†?
โ€ข ๐—ฆ๐—ฐ๐—ฎ๐—น๐—ฎ๐—ฏ๐—น๐—ฒ: Easily handles large data volumes (50K+ records).
โ€ข ๐—˜๐—ณ๐—ณ๐—ถ๐—ฐ๐—ถ๐—ฒ๐—ป๐˜: Supports indexing for fast, optimized queries.
โ€ข ๐—™๐—น๐—ฒ๐˜…๐—ถ๐—ฏ๐—น๐—ฒ: Simplifies updates and modifications, focusing on individual records without affecting others.

Have you faced a similar scenario? Share your approach in the comments below!
Iโ€™ve created a detailed post on ๐— ๐—ฒ๐—ฑ๐—ถ๐˜‚๐—บ ๐—ฏ๐—น๐—ผ๐—ด with step-by-step instructions and code examples. Feel free to check it out!๐Ÿ—ž๏ธ๐Ÿ“œ๏ธ: https://lnkd.in/deGK3W4E

Please repost this ๐Ÿ” if you find it useful. ๐Ÿ”” Follow Apurv Upadhyay โ˜๏ธ for more content like this

mongoDB#databaseDesign #scalability #datamodeling #NoSQL#developerTips #performanceOptimization #Tech

Top comments (0)