mcAngular2
mcAngular2

Reputation: 309

How to deal with huge amount of group-chat messages in MongoDB?

I'm building a chat app with different groups. Therefore I'm using a collection in Mongodb (one for all groups). This is my message schema:

   const MessageSchema = mongoose.Schema({
    groupId: Number,
    userId: Number,
    messageIat: Date,
    message: String,
    reactions: []
  });

Let's say I want to load the last 50 messages of the group with the id 10. To sort the messages I'm using the default ObjectId. I'm using the following query. For me, It seems like that I'm loading all messages of group 10, then sorting it to ensure the order and then I can limit the results. But this seems not very efficiently to me. If there are a lot messages it will cost quite some time right?

return Message.find({groupId:10}).sort( {_id: -1 }).limit(50)

My first try was to do the limit operation at first, but then I can not rely on the order so what's the commen way for this? Is it more common to split it up , so to have a collection per group?

Thanks for helping.

Upvotes: 1

Views: 514

Answers (1)

fardjad
fardjad

Reputation: 20424

According to docs:

For queries that include a sort operation without an index, the server must load all the documents in memory to perform the sort before returning any results.

So first off, make sure to create an index for whatever field you're going to sort the results by.

Also,

The sort can sometimes be satisfied by scanning an index in order. If the query plan uses an index to provide the requested sort order, MongoDB does not perform an in-memory sorting of the result set

Moreover, according to this page, the following queries are equivalent:

db.bios.find().sort( { name: 1 } ).limit( 5 )
db.bios.find().limit( 5 ).sort( { name: 1 } )

Finally, as longs as indices fit entirely in memory, you should be fine with your current approach. Otherwise you might want to consider doing some manual partitioning.

Upvotes: 2

Related Questions