I have a collection of messages:
{
messageid: ObjectId
userid: ObjectId
message: string
isread: true|false
}
and a collecti
MongoDB is very fast, so if your site is not high load you could simply recalculate the total number of messages and the total number of messages read on every request. Only put an index on isread. Count seems to be very fast, even on a large volume of data.
In fact, your total and unread fields are simply caches. Do you really need it ?
There is a question on a similar problem here : MongoDB: Calling Count() vs tracking counts in a collection