In this blog I will focus on explain how to perform common operations on a given collection in MongoDB.
Let consider a scenario where we have a messaging APP. An user can communicate with one or more users through the application and the system will
track all the communication in MongoDB.
Let consider a scenario where we have a messaging APP. An user can communicate with one or more users through the application and the system will
track all the communication in MongoDB.
We have a collection named “messages” in MongoDB.
Below is a sample documents that are stored in the collection:
Query #1 - Create document (User sends a message to another)
Query #2 - Message history between two users with the most recent message at the bottom
Query #3 - Retrieve all messages received by an user from other users and which are still unread by the receiving user
Query #4 - Retrieve all messages sent by an user to other users
Query #5 - Retrieve count of all messages sent by the user till date
Query #6 - Retrieve grouped count of unread messages per contact
The only problem with the above query is that the order of the result is not guaranteed. We want the results to contain the results where
the contact with the most recent communication comes at the top.
In order to achieve this order, we need to sort the result in descending order of ‘sendDate’ field.
The contact is nothing but the sender.
The above query can be rewritten as following :
Query #7: Get a unique list of Ids of all people that the user sent messages to
Query #7: Count of messages sent by the user to each of the contacts
Query #8: Get paginated list of messages sent between an user and another user [ Pagination Approach #1 : Using pageSize and pageNum ]
Query #9: Get list of messages sent between an user and another match [Pagination Approach #2 : Range based pagination]
Query #10: Update messages received by an an user from another user as read.
Most of the applications have some sort of badging functionality which is used to display certain counts to the users for CTA (Call To Ac...… Continue reading