我有一个注释集合,其中注释文本与元数据一起位于数组中.我喜欢评论文本数组中的分页.分页应该让我得到前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数组分页
发布评论