How do you design a data model for a social media platform using MongoDB

0 votes
With the help of proper code example can you tell me How do you design a data model for a social media platform using MongoDB?
Feb 22, 2025 in Node-js by Ashutosh
• 33,350 points
597 views

1 answer to this question.

0 votes

Design a MongoDB data model for a social media platform with the following collections:

Users:

{

  "_id": ObjectId,

  "username": String,

  "email": String,

  "passwordHash": String,

  "profilePic": String,

  "bio": String,

  "followers": [ObjectId], // Array of User IDs

  "following": [ObjectId] // Array of User IDs

}

Posts:

{

  "_id": ObjectId,

  "userId": ObjectId, // Reference to the User

  "content": String,

  "media": [String], // Array of URLs

  "timestamp": Date,

  "likes": [ObjectId], // Array of User IDs

  "comments": [

    {

      "userId": ObjectId,

      "text": String,

      "timestamp": Date

    }

  ]

}


Notifications:

{

  "_id": ObjectId,

  "userId": ObjectId, // Reference to the User

  "type": String, // e.g., "like", "comment", "follow"

  "sourceUserId": ObjectId, // User who triggered the notification

  "postId": ObjectId, // Optional, for post-related notifications

  "timestamp": Date,

  "read": Boolean

}

Messages:

{

  "_id": ObjectId,

  "senderId": ObjectId,

  "receiverId": ObjectId,

  "content": String,

  "timestamp": Date,

  "read": Boolean

}

answered Feb 23, 2025 by Kavya

Related Questions In Node-js

0 votes
1 answer

How do you design a schema for tree structures in MongoDB?

Designing a schema for tree structures in ...READ MORE

answered Feb 22, 2025 in Node-js by Kavya
543 views
0 votes
1 answer

How do you embed a document in MongoDB for better performance?

Embedding documents in MongoDB is a common ...READ MORE

answered Feb 22, 2025 in Node-js by Kavya
483 views
0 votes
1 answer

How would you implement a chat application using MongoDB’s data model patterns?

To implement a chat application using MongoDB’s ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
571 views
0 votes
1 answer

How do you run a js file using npm scripts?

Hello @kartik, Try this: { "scripts" : { ...READ MORE

answered Oct 12, 2020 in Node-js by Niroj
• 82,800 points
12,438 views
0 votes
1 answer

What is the difference between RDBMS relationships and MongoDB’s data model?

Feature RDBMS (SQL Databases) MongoDB (NoSQL Document Database) Data Structure Tables ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
477 views
0 votes
1 answer

Write a query for a compound index to optimize a search operation in MongoDB.

A compound index improves search performance by ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
535 views
0 votes
1 answer

How do you handle concerns for write operations in MongoDB?

Write operations in MongoDB need to be ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
538 views
0 votes
1 answer

Write a query to fetch specific fields using MongoDB projections.

Fetching Specific Fields Using MongoDB Projections MongoDB projections ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
533 views
0 votes
1 answer

How do you model a many-to-many relationship in MongoDB with an example?

In MongoDB, a many-to-many relationship can be ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
554 views
0 votes
1 answer

What are MongoDB data types, and how do you define them in a schema?

MongoDB supports various data types, including: String (String) ...READ MORE

answered Feb 23, 2025 in Node-js by anonymous
510 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP