PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 20, 2022

[FIXED] How to set character limits in content in ejs template

 October 20, 2022     ejs, node.js     No comments   

Issue

I am fetching content from Mognodb in EJS template. I have description field which contains more then 500 characters, but I want to show only 50 Characters in my view.

Can anyone tell me how to do it.


Solution

In the view you can use JavaScript String.prototype.substring():

<%= description.substring(0, 50) %>

Within MongoDB you can use $substr to return a new field with the desired 50 characters:

db.collectionName.aggregate([{
    $project: {
        title: 1,
        shortDescription: {
            $substr: ["$description", 0, 50]
        }
    }
}]);

Note that in the code example I am using a collection named collectionName and fields names like title and description.. This way will be returned only title and shortDescription with a limit to 50 characters to use in the view



Answered By - Yosvel Quintero
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing