I am new to Mongo. I wanted to atomically upsert a document based on multiple criteria. The document looks like the following:
{_id:..., hourOfTime:..., total:..., max:..., min:..., last:...}
This is basically hourly aggregation for number of clicks for an item identified by _id
. The clicks for each item is is flushed from the application to MongoDB every five seconds. So, the document need to be updated every five seconds.
Here is the case. Lets say at t=t0
, we have {_id:"nike", total:123, max:10, min:3, last:9}
then at t=t1
, I get message {_id:"nike", count: 12}
. Now, for _id="nike"
, I need to do the following,
- Increment
total
by12
- if
max < 12
, updatemax=12
- if
min > 12
, updatemin=12
- update
last=12
I want all this operation to be atomic. I unable to convert this in one single query. Any help/hint is appreciated.