Real use of computed pattern and Approximation pattern


I’ve read this article about computed pattern however it is very theoretical I’ve got a hard time to understand how to implement it. Let’s say I have a collection as the one below:

"User": {
    "_id": "<ObjectId>",
    "firstname": "<String>",
    "birthdate": "<ISODate>",
    "inscriptionDate": "<ISODate>",
    "lastConnectionDate": "<ISODate>",
    "totalFriends": "<Int>",
    "totalMessageSent": "<Int>"

TotalFriends computed only

For this example let’s exclude the approximation pattern.
totalFriends will be updated every time one user accept or remove a friend. How should I update this field ? Should I do a simple

    { _id: 1 },
    { $inc:{ totalFriends: 1}}

totalMessageSent computed + approximation

For example totalMessageSent will be updated every 10 messages sent. How should I update this field ?

Hi @Yoni_40785,

Great question! We have a great community forum where you can have an in-depth conversation about specific use cases and interesting problems that you get to solve using MongoDB. I would highly recommend posting your question there for a more involved discussion.


Oh okay my bad ! I though it was the right place, I’ll post it there.
Thank you :slight_smile:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.