简体   繁体   中英

Limit returned documents with Node.js & MongoDB

I'm attempting to limit the number of items returned with Mongodb. I tried User.find({limit:500} without luck. What is the correct way to get the last 500 documents?

    router.get("/dashboard", function(req, res){
    User.find({}, function (err, allUsers) {
    if (err) {
    console.log(err);
    } else {
    res.render("dashboard", {allUsers: allUsers});
    }
    });
    });

You can find more samples on https://mongoosejs.com/docs/queries.html

Basically you add the limit() function to the query

router.get("/dashboard", function(req, res){
User.find({}, function (err, allUsers) {
if (err) {
console.log(err);
} else {
res.render("dashboard", {allUsers: allUsers});
}
})
.limit(500);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM