mongoDB数组分页 [英] mongoDB array pagination

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

问题描述

我有一个注释集合,其中注释文本与元数据一起位于数组中.我喜欢评论文本数组中的分页.分页应该让我得到前5个"或下5个".在mongoDB中以良好的性能执行此操作的最佳方法是什么.

I have a comment collection with Comment Text in array with its Metadata. I like to the pagination in the Comment Text Array. The pagination should allow me to get the "previous 5" or "next 5". What is the best way to do this in mongoDB with good performance.

   { "_id" : ObjectId( "502c706ac2e6777781a593f6" ),
      "commentType":"loc",
      "commentList" : [ 
{ "commentText" : "HelloComment",
  "commentTS" : Date( 1345165636344 ),
  "commentLoc" : { "commentlat" : 0,
    "commentlong" : 0 },
  "userInfo" : { "$ref" : "userProfile",
    "$id" : ObjectId( "4fd182d92b2a87f780c6bbe8" ) } }, 
{ "commentText" : "2222222",
  "commentTS" : Date( 1345165636344 ),
  "commentLoc" : { "commentlat" : 0,
    "commentlong" : 0 },
  "userInfo" : { "$ref" : "userProfile",
    "$id" : ObjectId( "4fd182d92b2a87f780c6bbe8" ) } }, 
{ "commentText" : "3333333",
  "commentTS" : Date( 1345165636344 ),
  "commentLoc" : { "commentlat" : 0,
    "commentlong" : 0 },
  "userInfo" : { "$ref" : "userProfile",
    "$id" : ObjectId( "4fd182d92b2a87f780c6bbe8" ) } }, 
{ "commentText" : "44444444",
  "commentTS" : Date( 1345165636344 ),
  "commentLoc" : { "commentlat" : 0,
    "commentlong" : 0 },
  "userInfo" : { "$ref" : "userProfile",
    "$id" : ObjectId( "4fd182d92b2a87f780c6bbe8" ) } }, 
{ "commentText" : "5555555",
  "commentTS" : Date( 1345165636344 ),
  "commentLoc" : { "commentlat" : 0,
    "commentlong" : 0 },
  "userInfo" : { "$ref" : "userProfile",
    "$id" : ObjectId( "4fd182d92b2a87f780c6bbe8" ) } }, 
{ "commentText" : "6666666",
  "commentTS" : Date( 1345165636344 ),
  "commentLoc" : { "commentlat" : 0,
    "commentlong" : 0 },
  "userInfo" : { "$ref" : "userProfile",
    "$id" : ObjectId( "4fd182d92b2a87f780c6bbe8" ) } }, 
    { "commentText" : "777777",
  "commentTS" : Date( 1345165636344 ),
  "commentLoc" : { "commentlat" : 0,
    "commentlong" : 0 },
  "userInfo" : { "$ref" : "userProfile",
    "$id" : ObjectId( "4fd182d92b2a87f780c6bbe8" ) } }, 
    { "commentText" : "888888",
  "commentTS" : Date( 1345165636344 ),
  "commentLoc" : { "commentlat" : 0,
    "commentlong" : 0 },
  "userInfo" : { "$ref" : "userProfile",
    "$id" : ObjectId( "4fd182d92b2a87f780c6bbe8" ) } }, 
    { "commentText" : "6666666",
  "commentTS" : Date( 1345165636344 ),
  "commentLoc" : { "commentlat" : 0,
    "commentlong" : 0 },
  "userInfo" : { "$ref" : "userProfile",
    "$id" : ObjectId( "4fd182d92b2a87f780c6bbe8" ) } }, 
{ "commentText" : "999999",
  "commentTS" : Date( 1345165668809 ),
  "commentLoc" : { "commentlat" : 0,
    "commentlong" : 0 },
  "userInfo" : { "$ref" : "userProfile",
    "$id" : ObjectId( "4fdeb271de26fd298262bb84" ) } }, 
{ "commentText" : "HelloComment",
  "commentTS" : Date( 1345749858601 ),
  "commentLoc" : { "commentlat" : 0,
    "commentlong" : 0 },
  "userInfo" : { "$ref" : "userProfile",
    "$id" : ObjectId( "4fdeb271de26fd298262bb84" ) } }, 
{ "commentText" : "NewTestforcomments",
  "commentTS" : Date( 1345750020526 ),
  "commentLoc" : { "commentlat" : 0,
    "commentlong" : 0 },
  "userInfo" : { "$ref" : "userProfile",
    "$id" : ObjectId( "4fdeb271de26fd298262bb84" ) } } ],
   } } 

感谢您的帮助.

推荐答案

您可以使用$slice执行此操作:

You can do this with $slice: http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields#RetrievingaSubsetofFields-RetrievingaSubrangeofArrayElements

这里还有一个slice的js测试示例,其中包括几个示例: https://github.com/mongodb/mongo/blob/master/jstests /slice1.js

There's also a js test example of slice here, with several examples: https://github.com/mongodb/mongo/blob/master/jstests/slice1.js

因此,findOne$slice结合使用可能是可行的方法.

So, findOne, combined with $slice, is probably the way to go.

在没有有关您所使用语言的更多信息的情况下,我认为这是我能做的最深的事情.

Without more info about the language you're using, I think that's the deepest I can go.

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

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