MongoDB fixed size array implementation

可紊 提交于 2019-12-06 12:08:01

问题


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 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?


回答1:


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.



来源:https://stackoverflow.com/questions/4517950/mongodb-fixed-size-array-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!