"US Virgin Islands": [
"Charlotte Amalie",
"Christiansted",
"Frederiksted",
"Kingshill",
"St John Island"
],
我有这个JSON文件。我想按原样保存数据。因此,国家名称及其所有城市。我创建了一个名为address的集合,但我不知道如何正确设置属性。我正在使用MongoDB作为数据库。我想创建json模型。
"name": "address",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"country": {
"type": "object",
"required": true
}
},
答案 0 :(得分:0)
如果要在地址模型中使用相关模型,则需要使用关系。我建议使用环回cli来做到这一点。另外,请查看回送文档中的relations章节以获取有关关系的更多信息。
假设您拥有国家/地区模型,则地址模型将定义为:
{
"name":"address",
"base":"PersistedModel",
"idInjection":true,
"options":{
"validateUpsert":true
},
"relation": {
"country": {
"type": "hasOne",
"model": "country"
"foreignKey": ""
}
}
}
此外,您需要在地址模型中创建一个EmiratesTo关系:
{
"name":"country",
"base":"PersistedModel",
"idInjection":true,
"options":{
"validateUpsert":true
},
"properties": { ... }
"relation": {
"address": {
"type": "belongsTo",
"model": "address"
"foreignKey": ""
}
}
}
它将在您的地址模型中自动创建一个字段countryId
。然后,如果您想使用整个国家/地区模型实例(而不只是countryId
)加载地址,则需要调用以下代码:
Address.find({where: {id: someId}, include: 'country'})