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

还有一个这里切片的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天全站免登陆