A compound index on senderId, receiverId, and createdAt (descending). This optimizes the most common query fetching message history between two users in reverse chronological order with pagination.
DevTinder Database Design and Schema MongoDB Collections and Relationships
What index does the Message collection use?
The answer
More FAQs in DevTinder Database Design and Schema MongoDB Collections and Relationships
Three collections: users (user profiles with name, email, password, photo, skills, about), connectionrequests (connection requests with from, to, status), and messages (chat messages with sender, receiver, text).
Using ObjectId references (ref) in Mongoose. ConnectionRequest references two users (fromUserId, toUserId). Message references sender and receiver. Use populate() to fetch the full user document when needed.
The compound unique index on fromUserId and toUserId prevents duplicate connection requests between the same pair of users at the database level, ensuring data integrity even if the application logic fails.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.