Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB: best design for messaging app [duplicate]

Tags:

A very simple design problem. Say I want to build Facebook Messenger. Let's say John and Marry are chatting, which is a better approach?

1) 1 document per conversation, messages is an array of message object

{ participants: ['john', 'marry'],    messages: [        { sender: 'john', content: 'howdy', time_created: new Date() },       { sender: 'marry', content: 'good u', time_created: new Date() },       ...   ] } 

2) 1 document per message

{ participants: ['john', 'marry'], sender: 'john', message: 'howdy', time_created: new Date() } // document 1 { participants: ['john', 'marry'], sender: 'marry', message: 'good u', time_created: new Date() } // document 2 ....  

Which approach has better performance in terms of inserting a new message (updating a conversation vs. creating a new document) ?

or are there any better approach (as in my 2nd approach, i'm not sure if it's a good design to specify the participants field in each document)?

Thanks!

like image 691
Maria Avatar asked Jun 13 '15 21:06

Maria


People also ask

Is MongoDB good for chat application?

MongoDB is used for many chat applications. The primary benefits associated with the document model enable you to seamlessly extend your application as you build functionality.

What is MongoDB best suited for?

MongoDB works best with unstructured data, so it's great for Big Data systems, MapReduce applications, news site forums, and social networking applications. Use MongoDB when: You're using cloud computing. MongoDB is ideal for cloud computing.

What the most important consideration while designing the schema for MongoDB?

General Rules for MongoDB Schema Design: Rule 1: Favor embedding unless there is a compelling reason not to. Rule 2: Needing to access an object on its own is a compelling reason not to embed it. Rule 3: Avoid joins and lookups if possible, but don't be afraid if they can provide a better schema design.

What makes MongoDB the best?

High performance (speed) Thanks to the document model used in MongoDB, information can be embedded inside a single document rather than relying on expensive join operations from traditional relational databases. This makes queries much faster, and returns all the necessary information in a single call to the database.


1 Answers

Based on your example data for the messaging app, what you could do is having two collections: Conversation and Messages. Where the relationship is one Conversation have many Messages.

Conversation: { id: 123   participants: ['john', 'marry'], }   Message: { sender: 'john',    content: 'howdy',    time_created: new Date(),   converstationId: 123 }, { sender: 'marry',    content: 'good u',    time_created: new Date(),   converstationId: 123  }, 

Creating a new document message would be better in this case, as you can then have two applications (1 for john and 1 for marry) without handling the possibility of the two of them updating the same document. They just happens to be sharing the same conversation session.

Also, if a conversation is a single document, you might end up with a very large document. (Document growth concern)

You can find out more about data modelling for this mongodb doc

http://docs.mongodb.org/manual/core/data-modeling-introduction/

Also see MongoDB: Socialite for examples/discussion for social network use case.

Hope it helps. Cheers.

like image 68
Wan Bachtiar Avatar answered Oct 25 '22 18:10

Wan Bachtiar