MongoDB固定大小数组实现 [英] MongoDB fixed size array implementation

查看:159
本文介绍了MongoDB固定大小数组实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与 http://jira.mongodb.org/browse/SERVER-991有关.我需要一个固定大小的嵌套数组,用于存储与当前文档相关的最新XX个事件.

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. 从元素中选择计数字段
  2. 将元素推送到嵌入式数组
  3. 如果计数< XX,然后是inc_count;否则,弹出最新元素;

这种方法的缺点:

    每次事件推送
  • 3个查询
  • 由于mongo没有事务,因此数组中的元素可能比允许的少或多(同一时间,两个客户端push或pop元素)-但这不会给我带来很大的麻烦

您能评论一下如何实现吗?

Could you comment on how this could be implemented?

推荐答案

您可以使用空值预先初始化数组并摆脱大小检查.只需弹出并推动即可.这样,您还可以避免大小增长和文档重定位.您的客户端代码将必须正确处理以null终止的数组.

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.

这篇关于MongoDB固定大小数组实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆