mongoDB数组分页

编程入门 行业动态 更新时间:2024-10-24 21:19:57
本文介绍了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执行此操作: www.mongodb/display/DOCS/Retrieving+a+Subset+of+Fields#RetrievingaSubsetofFields-RetrievingaSubrangeofArrayElements

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

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

There's also a js test example of slice here, with several examples: github/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数组分页

本文发布于:2023-11-22 15:14:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617929.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分页   数组   mongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!