我的网址目前如下所示:
http://www.sitename.com/watch?companyId=507f1f77bcf86cd799439011&employeeId=507f191e810c19729de860ea&someOtherId=.....
所以,正如你所看到的,它变得相当长、非常快。
我正在考虑缩短这些 ObjectId。
我的想法是,我应该向数据库中的每个模型添加名为“shortId”的新字段。所以不要有:
var CompanySchema = mongoose.Schema({
/* _id will be added automatically by mongoose */
name: {type: String},
address: {type: String},
directorName: {type: String}
});
我们会有这样的:
var CompanySchema = mongoose.Schema({
/* _id will be added automatically by mongoose */
shortId: {type: String}, /* WE SHOULD ADD THIS */
name: {type: String},
address: {type: String},
directorName: {type: String},
});
我找到了一种方法来做到这一点:
// Encode
var b64 = new Buffer('47cc67093475061e3d95369d', 'hex')
.toString('base64')
.replace('+','-')
.replace('/','_')
;
// -> shortID is now: R8xnCTR1Bh49lTad
但我仍然认为它可以更短。
另外,我发现了这个 npm 模块:https://www.npmjs.com/package/short-mongo-id https://www.npmjs.com/package/short-mongo-id但我没有看到它被使用太多,所以我无法判断它是否可靠。
有人有什么建议吗?