Assume we have 3 services which works in parallel and doing writes to our MongoDB storage. They create the records, which contain following info:
{
guid: GUID,
ts: Timestamp,
data: Object
}
As result in the MongoDB storage in one moment of time should be either nothing for particular GUID, or the LATEST (max ts) record.
Small example:
- 1 call with
{guid: 1, ts: 10, data: {}}- inserted{guid: 1, ts: 10, data: {}} - 2 call with
{guid: 1, ts: 5, data: {}}- nothing updated - 3 call with
{guid: 1, ts: 15, data: {}}- document updated with ts: 15 and new data.
In other words, we have to insert the record if there is no such document with provided GUID, and update the record in case when such guid already exist and the ts is greater then in existed record. DO NOT update the record, if ts is less then in existed record.
I understand that this is some kind of upsert operation, but I can't imagine how to deal with this. Tried to use findAndModify, mapReduce or $max update operator, but no luck atm. Thank you in advance.
tsanddataset on the document?