我有一个环回应用程序,它在创建模型时提供诸如“56dbfa7089223aca7946ca14”之类的 ID。我更喜欢像“0”或“73”这样的ID。有没有办法调整 id 注入(inject)实践,使 id 从 0 开始并以 10 为基数递增?
数据存储是MongoDB v2.6.10) 在 Ubuntu 15.10 上的 Node v5.7.1 上运行环回 v2.22
这是相关的 model.json
{
"name": "Term",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
},
"beginDate": {
"type": "date",
"required": true
},
"endDate": {
"type": "date",
"required": true
}
},
"validations": [],
"relations": {
"lessons": {
"type": "hasMany",
"model": "Lesson",
"foreignKey": ""
},
"classes": {
"type": "hasMany",
"model": "Class",
"foreignKey": ""
},
"weeklySchedules": {
"type": "hasMany",
"model": "WeeklySchedule",
"foreignKey": ""
}
},
"acls": [],
"methods": {}
}
最佳答案
有几种方法可以做到这一点,您可以在官方documentation中阅读它们
mongo开发者表示,使用自己的id是完全可以的,如果你确实需要它,那就用吧。
但是,如果您这样做只是因为“不想要 ObjectId,因为它看起来不太好”,我建议您习惯它。
关于node.js - 在进行 id 注入(inject)时,如何让 Loopback 优先选择短 id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35825489/