0

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

Community
  • 1
  • 1
  • Did you try with `filter` https://docs.mongodb.com/manual/reference/operator/aggregation/filter/ – Ruhul Amin Mar 21 '17 at 16:36
  • Filter returns the document which fulfill a certain criteria but it i want to return all documents but sliced >> eg. if the document has entry like 'this is very long text to show' , i want to return only 'this is very ..' – Rakesh Soni Mar 21 '17 at 16:58
  • 1
    Possible duplicate of [Match with substring in mongodb aggregation](http://stackoverflow.com/questions/20066279/match-with-substring-in-mongodb-aggregation) – Rakesh Soni Mar 21 '17 at 17:22

1 Answers1

1

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 
    });
});
EmptyArsenal
  • 7,314
  • 4
  • 33
  • 56
Shumi Gupta
  • 1,505
  • 2
  • 19
  • 28
  • it limits json object not the length of data ie. if previously it was returning 100 records, now it will returning only 10 records.....as per this code – Rakesh Soni Mar 21 '17 at 16:50
  • so what else you want do you want is like it should return you 100 documents and length of data should be 10?. whats the logic behind this and please clear what exactly you want so we can help. – Shumi Gupta Mar 21 '17 at 17:17
  • found a solution, i was looking for some like http://stackoverflow.com/questions/20066279/match-with-substring-in-mongodb-aggregation @ShumiGupta – Rakesh Soni Mar 22 '17 at 09:15