2017-04-19 52 views
0

我正在尝试使用MongoDB来实现Loopback 3.0.0的组合ID。每对product_id/keyword应该是唯一的......如何在Loopback中使用组合ID?

我检查的官方文档:

https://loopback.io/doc/en/lb3/Model-definition-JSON-file.html#data-mapping-properties

这里我的模型:

{ 
    "name": "comparative", 
    "plural": "comparative", 
    "base": "PersistedModel", 
    "idInjection": true, 
    "options": { 
    "validateUpsert": true 
    }, 
    "properties": { 
    "product_id": { 
     "id": true, 
     "type": "string", 
     "required": true 
    }, 
    "keyword": { 
     "id": true, 
     "type": "string", 
     "required": true 
    } 
    }, 
    "validations": [], 
    "relations": {}, 
    "acls": [], 
    "methods": {} 
} 

虽然,我所有的努力,使复合IDS product_id /关键字失败。

发帖时:

// 1st POST Success. 
{ 
    "product_id": "string", 
    "keyword": "string" 
} 
// 2nd POST Success. 
{ 
    "product_id": "string1", 
    "keyword": "string" 
} 
// 3rd POST FAILED -> I want it to success 
{ 
    "product_id": "string", 
    "keyword": "string1" 
} 

任何想法?

即使在那之后,我需要保留一个自动生成的MongoDB id只是为了跟踪对象。

我试着用“idInjection”,不能与复合ID一起工作......(不会产生任何东西......) 如果我添加另一个字段“id”,生成的设置为true,复合id不起作用所有(相反由于其开展部分之前)

谢谢

回答

0

你不必把id: true的性能。这只对单个字段完成,如果你不想要默认标识即。 _id在蒙戈

"properties": { 
    "product_id": { 
     "type": "string", 
     "required": true 
    }, 
    "keyword": { 
     "type": "string", 
     "required": true 
    } 
    } 

我假设你有你想要涉及这个模型,另一型号产品。为了实现这一点,增加关系

检查this环回文档这样做。

希望对您有所帮助