Rakesh Soni
Rakesh Soni

Reputation: 41

limit the data returned by model.find() in mongodb

I want to limit the length of data returned by Model.find() in mongodb/mongoose

here is my code Want to return 'Excerpt' from content.

Blog.find({},{
    title: 1,
    content: 1 // basically wants to return content not more than 200 character
    }, function(err, data){
    if (err) {
        console.log(err);
    }
    res.render('blog/posts', {
        title:'All posts',
        posts: data 
    });
});

In other words how to return limited content from mongoDB

Update Found solution:

Match with substring in mongodb aggregation

Upvotes: 0

Views: 209

Answers (1)

Shumi Gupta
Shumi Gupta

Reputation: 1525

You just need to pass limit parameter in 3rd option

Blog.find({},{
        title: 1,
        content: 1 // basically wants to return content not more than 200 character
    },{ limit:10 }, function(err, data){
    if (err) {
        // You should handle this err because res.render will send
        // undefined if you don't.
        console.log(err);
    }
    res.render('blog/posts', {
        title:'All posts',
        posts: data 
    });
});

Upvotes: 1

Related Questions