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

0 votes
Can i know How would you implement a chat application using MongoDB’s data model patterns?
Feb 22 in Node-js by Ashutosh
• 20,830 points
48 views

1 answer to this question.

0 votes

To implement a chat application using MongoDB’s data model patterns, follow these key steps:

1. Schema Design

Users Collection (Referencing Pattern)

Stores user details and chat references.

{

  "_id": ObjectId("user1_id"),

  "username": "Ram",

  "contacts": [{ "user_id": ObjectId("user2_id"), "chat_id": ObjectId("chat1_id") }]

}

Chats Collection (Embedding for metadata, Referencing for messages)

Stores chat metadata and participants.

{

  "_id": ObjectId("chat1_id"),

  "type": "private",

  "participants": [ObjectId("user1_id"), ObjectId("user2_id")],

  "last_message": { "text": "Hello!", "sender": ObjectId("user1_id"), "timestamp": ISODate("2025-02-22T10:30:00Z") }

}

Messages Collection (Bucket Pattern)

Groups messages by date for efficient retrieval.

{

  "_id": ObjectId("bucket1"),

  "chat_id": ObjectId("chat1_id"),

  "date": "2025-02-22",

  "messages": [

    { "sender": ObjectId("user1_id"), "text": "Hello!", "timestamp": ISODate("2025-02-22T10:30:00Z") },

    { "sender": ObjectId("user2_id"), "text": "Hi!", "timestamp": ISODate("2025-02-22T10:31:00Z") }

  ]

}

2. Indexing for Performance

db.messages.createIndex({ "chat_id": 1, "messages.timestamp": -1 });

3. API Implementation (Node.js + Express + MongoDB)

Send Message

app.post('/send', async (req, res) => {

    const { chatId, senderId, text } = req.body;

    await Message.findOneAndUpdate(

        { chat_id: chatId, date: new Date().toISOString().split('T')[0] },

        { $push: { messages: { sender: senderId, text, timestamp: new Date() } } },

        { upsert: true }

    );

    res.send("Message sent!");

});

Get Messages

app.get('/messages/:chatId', async (req, res) => {

    const messages = await Message.find({ chat_id: req.params.chatId }).sort({ "messages.timestamp": -1 }).limit(10);

    res.json(messages);

});
answered Feb 23 by Kavya

Related Questions In Node-js

0 votes
1 answer

How would you model a one-to-many relationship in MongoDB?

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

answered Feb 22 in Node-js by Kavya
46 views
0 votes
1 answer

How do you implement routing in a React application?

Implementing Routing in a React Application (React ...READ MORE

answered Feb 23 in Node-js by Kavya
68 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,840 points
11,697 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to set read concern in MongoDB?

In MongoDB, you can set read concern ...READ MORE

answered Feb 23 in Node-js by Kavya
59 views
0 votes
1 answer
0 votes
1 answer
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