Is there any way to atomically update two collections in MongoDB?

后端 未结 5 1254
轻奢々
轻奢々 2020-12-08 08:45

I have a collection of messages:

{
    messageid: ObjectId
    userid: ObjectId
    message: string
    isread: true|false
}

and a collecti

5条回答
  •  不思量自难忘°
    2020-12-08 09:05

    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

提交回复
热议问题