开发者

MongoDB fixed size array implementation

开发者 https://www.devze.com 2023-02-01 02:53 出处:网络
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.

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:

  1. Select count field from element
  2. Push element to embedded array
  3. 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 sam开发者_运维问答e 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消