我在MongoDB数组中遇到分页问题。我有一个包含以下数据的模式
{
"_id" : ObjectId("5aa25dd9a73b615c35505066"),
"Fbid" : "1661115317267347",
"__v" : 0,
"events" : [
{
"_id" : ObjectId("5aa25dd9500cb334486df5b7"),
"event" : "selfie",
"imageId" : "9s3ix5n3mwm"
},
{
"_id" : ObjectId("5aa25dfa500cb334486df5b8"),
"event" : "selfie",
"imageId" : "32df8pdjh9c"
},
{
"_id" : ObjectId("5aa25e6e619354309043cb3c"),
"event" : "selfie",
"imageId" : "1g49hicor87"
},
{
"_id" : ObjectId("5aa25f4c4351732fb837d9b3"),
"event" : "selfie",
"imageId" : "q49c8g5l7k"
},
{
"_id" : ObjectId("5aa25f98c2b85010f4fca6dd"),
"event" : "selfie",
"imageId" : "pj3467cz5zc"
},
{
"_id" : ObjectId("5aa29ead0a72530af0a468e6"),
"event" : "selfie",
"imageId" : "6wduwcs6lvr"
},
{
"_id" : ObjectId("5aa29ec70a72530af0a468e7"),
"event" : "selfie",
"imageId" : "g53i9c1ubf5"
},
{
"_id" : ObjectId("5aa29f840a72530af0a468e8"),
"event" : "selfie",
"imageId" : "m0sdyzyl9l"
},
{
"_id" : ObjectId("5aa29f9a0a72530af0a468e9"),
"event" : "selfie",
"imageId" : "mwo0k55deyo"
},
{
"_id" : ObjectId("5aa29fa10a72530af0a468ea"),
"event" : "selfie",
"imageId" : "w4lnpkr1z8f"
},
{
"_id" : ObjectId("5aa29faa0a72530af0a468eb"),
"event" : "selfie",
"imageId" : "wxy9uj0trcm"
},
{
"_id" : ObjectId("5aa29fb20a72530af0a468ec"),
"event" : "selfie",
"imageId" : "odpuibgotl9"
}
],
"email" : "1407258@kiit.ac.in"
}
架构中存在此类型的多个对象。在分页部分,我希望页面在页面上一次显示数组的一部分,即模式中的10个事件数组。但是,接下来的2个数组元素应该与下一个对象的数组一起显示在下一页上。我该如何实施呢?
我见过一些slice
,但无法实现它。任何帮助将不胜感激。
答案 0 :(得分:0)
对于分页,您可以使用skip()
和limit()
运算符。
// Page 1
db.foo.find().limit(10)
// Page 2
db.foo.find().skip(10).limit(10)
// Page 3
db.foo.find().skip(10).limit(10)
答案 1 :(得分:0)
嗯,我无法实现我想要的东西。不得不改变整个数据库结构,最后分页正在发生。 早先将图像id存储在数组中,现在为它制作一个单独的文档。 这是结构
{
"_id" : ObjectId("5aa39904ac370621286099f6"),
"Fbid" : "113161819515501",
"imageId" : "9s76au1zrfs",
"email" : "pigspdsude_1519876920@tfbnw.net",
"event" : "selfie",
"__v" : 0}
分页代码:
likeImformation.paginate( likeImformation.find({"event":event}),{page:page, limit:5},function(err, data){
if(data===undefined){
res.render('event.hbs', {
name: req.user.displayName
});
}
else{
console.log("*******************************************");
console.log(data.docs+"*************"+"alpit");
console.log("*******************************************");
res.render('event.hbs', {
name: req.user.displayName,
event:data.docs,
eventName:req.params.event,
});
}
})
希望它可以帮助某人..: - )