gpt4 book ai didi

javascript - Mongoskin 中的分页(跳过 + 限制)

转载 作者:行者123 更新时间:2023-11-30 17:34:15 26 4
gpt4 key购买 nike

通过查看 mongodb 示例,我尝试了一些操作,例如:

// returns an empty array
funnyPosts.find({limit:5}).toArray(function(err, result) {
console.log(result);
});

也试过

// returns { _construct_args: [],

console.log(db.collection('funny_posts').find().skip(3).limit(3));

最佳答案

您是否正在尝试使用 Node.js 和 MongoDB 实现分页?

您的参数位置错误(不是 find() 的第一个参数)。这是我的 HackHall 代码 GitHub link :

exports.getPosts = function(req, res, next) {
var limit = req.query.limit || LIMIT;
var skip = req.query.skip || SKIP;
req.db.Post.find({}, null, {
limit: limit,
skip: skip,
sort: {
'_id': -1
}
}, function(err, obj) {
if (!obj) next('There are not posts.');
obj.forEach(function(item, i, list) {
if (req.session.user.admin) {
item.admin = true;
} else {
item.admin = false;
}
if (item.author.id == req.session.userId) {
item.own = true;
} else {
item.own = false;
}
if (item.likes && item.likes.indexOf(req.session.userId) > -1) {
item.like = true;
} else {
item.like = false;
}
if (item.watches && item.watches.indexOf(req.session.userId) > -1) {
item.watch = true;
} else {
item.watch = false;
}
});
var body = {};
body.limit = limit;
body.skip = skip;
body.posts = obj;
req.db.Post.count({}, function(err, total) {
if (err) next(err);
body.total = total;
res.json(200, body);
});
});
};

有关更多信息,请查看我在 webapplog.com 上的其他帖子和 Express.js Guide .

关于javascript - Mongoskin 中的分页(跳过 + 限制),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22364858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com