我有20万个文档的集合,每个包含“客户名称”字段。有大约1k独特的“customerName”值。 (该字段建立索引)猫鼬 - 呈现不同的价值观和slugify这些值
我需要使这些客户的名单,并为每一个蛞蝓,这样我可以在我的路由URL中使用。
下一步将呈现一个页面,显示所有包含客户名称的文件每个客户名称。
这里是我迄今为止,
/// Customer.js
const rmaSchema = new Schema({
CustomerName: { type: String, index: true },
slug: String },
{ collection : 'mycompany' // collection name
});
rmaSchema.pre('save', function(next) {
this.slug = slugify(this.CustomerName) ;
next();
});
const rmaModel = mongoose.model('Rma', rmaSchema);
module.exports = rmaModel;
// function to slugify a name
function slugify(text) {
return text.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
}
/// Customer.Controller.js
function showCustomers(req, res){
Rma.distinct('CustomerName', function(err, customers) {
if (err){
res.status(404);
res.send('customers not found!');
}
res.render('pages/customers', {customers: customers});
});
};
module.exports = showCustomers;
/// customer.ejs
<table class="table table-bordered table-hover table-striped">
<tbody>
<% for (var customer of customers) { %>
<tr>
<td><%= customer.CustomerName %></td>
<td><a href="/events/<%= customer.slug %>" class="btn btn-sm btn-primary">Generate Report</a></td>
</tr>
<% } %>
</tbody>
slu supposed不应该是独一无二的? – Mikey