My question is related to http://jira.mongodb.org/browse/SERVER-991. I need a nested array of a fixed size storing latest XX events related to current document.
How can I implement client-side this feature? I thought of maintaining a count on array size, something like:
- Select count field from element
- Push element to embedded array
- If count < XX, then inc_count; Else, pop latest element;
The downsides of this approach:
- 3 queries for each event push
- as mongo doesn't have transactions, the array could have either less or more elements than allowed(in the same time, two clients push or pop elements) - but this doesn't bother me very much
Could you comment on how this could be implemented?
You can preinitialize arrays with nulls and get rid of size checks. Just pop and push. This way you also avoid size growth and document relocations. Your client code will have to handle null-terminated arrays properly.