I have a collection of messages:
{
messageid: ObjectId
userid: ObjectId
message: string
isread: true|false
}
and a collection of message counts per user:
{
userid: ObjectId
total: int
unread: int
}
When I delete a message from "messages" collection, I also need to decrease "total" in "counts" collection, and conditionally (if "messages.isread" == false) decrease "unread" field as well.
For that I need to first retrieve teh message, check its "isread" field, and then update the counts. There is a possibility that the message will be marked as read in between those actions, then I will decrease "unread" counts incorrectly.
Is there a way to conditionally change something in one collection based on results of other collection in one shot?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…