在视图中,您可以使用 JavaScript String.prototype.substring():
<%= description.substring(0, 50) %>
Run Code Online (Sandbox Code Playgroud)
在 MongoDB 中,您可以使用$substr返回具有所需 50 个字符的新字段:
db.collectionName.aggregate([{
$project: {
title: 1,
shortDescription: {
$substr: ["$description", 0, 50]
}
}
}]);
Run Code Online (Sandbox Code Playgroud)
请注意,在代码示例中,我使用了一个名为collectionName和字段名称的集合,如title和description.. 这种方式将仅返回,title并且shortDescription在视图中使用限制为 50 个字符