zahid ansari
Building scalable systems by applying mental models to code and architecture.
Website
Install this app to stay connected and in the loop.
Building meaningful experiences one pixel at a time ๐.
Made with by Zahid
views
zahid ansari
Building scalable systems by applying mental models to code and architecture.
Website
Install this app to stay connected and in the loop.
Building meaningful experiences one pixel at a time ๐.
Made with by Zahid
views

Share
NoSQL doesn't mean no structure. Best practices for modeling relationships in MongoDB to avoid performance bottlenecks.
"Schema-less" is a myth in production. If you don't design your MongoDB schema with access patterns in mind, your SaaS will crawl as it scales.
I built a project management tool where I embedded every "Comment" inside the "Task" document. It was great... until a task had 5,000 comments. The document size limit hit, and pulling a task meant pulling megabytes of text data I didn't need.
1// User Model
2{
3 _id: ObjectId("..."),
4 name: "Zahid",
5 email: "zahid@example.com"
6}
7
8// Project Model
9{
10 _id: ObjectId("..."),
11 title: "Website Redesign",
12 ownerId: ObjectId("...") // Reference to User
13}
14
15// Task Model
16{
17 _id: ObjectId("..."),
18 projectId: ObjectId("..."), // Reference to Project
19 title: "Fix Homepage CSS",
20 assignedTo: [ObjectId("...")] // Array of User References
21}1import mongoose, { Schema } from 'mongoose';
2
3const TaskSchema = new Schema({
4 title: String,
5 project: { type: Schema.Types.ObjectId, ref: 'Project' },
6 // Indexing is crucial for performance!
7}, { timestamps: true });
8
9// Check if model exists before compiling (Next.js Hot Reload Fix)
10export const Task = mongoose.models.Task || mongoose.model('Task', TaskSchema);1projectId1.lean()Modeling data in MongoDB requires thinking about how you will read the data. Design for your most frequent queries.
Tech Stack: MongoDB, Mongoose, Next.js