如何在mongo集合中获取子文档数组的分页/切片数据? [英] How to get paginated/sliced data of subdocument array in mongo collection?

查看:198
本文介绍了如何在mongo集合中获取子文档数组的分页/切片数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的收藏集:

I have a collection like this:

{
"_id" : ObjectId("51f4ad560364f5490ccebe26"),
"fiTpcs" : [
    "uuid1",
    "uuid2",
    "uuid3",
    "uuid4",
    "uuid5"
],
"fiTpcsCnt" : 5
}

fiTpcs的列表很长,以后可以转到数百个.检索集合时,我想获得有限的fiTpc列表,一次说20个,并分别发出请求以从fiTpcs获取后续数据.我只想确保当我有更多数据时查询不会变慢.有没有办法在mongodb中做到这一点?直到现在,我一直在做

The list of fiTpcs is long and can go to hundreds later. When I retrieve my collection, I want to get a limited list of fiTpcs, say 20 at a time and fire separate requests to get subsequent data from fiTpcs. I just want to ensure that the queries don't get slow later when I have a lot more data. Is there a way to do it in mongodb? until now, I have been doing

db.userext.find({"_id" : ObjectId("51f4ad560364f5490ccebe26")}).pretty();

这总是让我获得完整的fiTpcs数组.我在Spring中使用Java驱动程序,并且使用Spring/java的解决方案也可以.请注意-如果该解决方案要求mongo扫描整个fiTpcs阵列然后切成一部分,那么它并没有真正增加任何性能上的好处,这不是我想要的.

which always gets me the full fiTpcs array. I am using java driver with Spring and a solution using Spring/java would also be fine. Please note - if the solution requires mongo to scan through the whole fiTpcs array and then slice a part of it, it doesn't really add any performance benefits, that is not what I am looking for.

推荐答案

经过几天的思考/尝试了各种选择,这才是我最终要做的.我这样修改了我的文档:

After a couple of days of thinking/trying various options, this is what I did finally. I modified my document like this:

{
  "_id" : ObjectId("51f4ad560364f5490ccebe26"),
  "page" : 1,  //1 is the default
  "slug" : "some-unique-string-identifier"
  "fiTpcs" : [
    "uuid1",   //these could be long text, like a long comment/essay
    "uuid2",
    "uuid3",
    "uuid4",
    "uuid5"
  ],
  "fiTpcsCnt" : 5
}

我在memcached中保留了"pageCount"和"totalFiTpcsCnt".我已将MAX_FITPCSCNT设置为500(目前为500,实验性).创建类型为userext的新文档时,将页面值设置为1.

I keep a "pageCount" and "totalFiTpcsCnt" in memcached. I have set MAX_FITPCSCNT = 500 (500 for now, experimental). When I create a new document of type userext, I set the page value to 1.

如果我必须将新对象推送到fiTpcs数组:

If I have to push a new object to fiTpcs array:

1)检查"totalFiTpcsCnt"是否为500的倍数.如果是,请创建一个新的userext类型的文档,该文档具有相同的参数,fiTpcsCnt为0,fiTpcs数组为null. 2)更新最后一个userext-通过slug和"pageCount"查询,推送到fiTpcs.逐出"pageCount"和"totalFiTpcsCnt"的缓存.

1) check if "totalFiTpcsCnt" is a multiple of 500. If yes, create a new document of type userext with the same slug, fiTpcsCnt as 0 and fiTpcs array as null. 2) update the last userext - query by slug and "pageCount", push to fiTpcs. Evict cache for "pageCount" and "totalFiTpcsCnt".

每当我需要userext文档时,我总是只浏览第一页.这样,我永远不需要一次查询超过500个fiTpcs类型的对象,并且仍然会始终在memcached中始终更新totalFiTpcsCnt.

Whenever I need my userext document, I always take just the first page. This way I'll never need to query for more than 500 objects of type fiTpcs at a time and I will still have totalFiTpcsCnt always updated in memcached.

这篇关于如何在mongo集合中获取子文档数组的分页/切片数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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