loopbackio / loopback-connector-mongodb

The official MongoDB connector for the LoopBack framework.
https://loopback.io/doc/en/lb4/MongoDB-connector
Other
187 stars 236 forks source link

Nested Foreign Keys #180

Closed grindfuk closed 7 years ago

grindfuk commented 9 years ago

Hi, Just started using loopback and was wondering: Can foreign keys be nested in a mongo document? For example, can you have

{
  "name": "supplier",
 ...
  "relations": {
    "supplier_acct": {
      "type": "hasOne",
      "model": "account",
      "foreignKey": "suppliers.supplierId"
    }
 ...
}
ainesophaur commented 9 years ago

The foreign key would just be supplierId. If you don't provide a specific foreign key then loopback will set it to model + "Id"

Your mongo entry should then have just the regular properties along with the ObjectId of the relation target

If I'm misunderstanding your question please let me know. On Oct 8, 2015 7:41 PM, "grindfuk" notifications@github.com wrote:

Hi, Just started using loopback and was wondering: Can foreign keys be nested in a mongo document? For example, can you have { "name": "supplier", ... "relations": { "supplier_acct": { "type": "hasOne", "model": "account", "foreignKey": "suppliers.supplierId" } ... }

— Reply to this email directly or view it on GitHub https://github.com/strongloop/loopback-connector-mongodb/issues/180.

grindfuk commented 9 years ago

Thanks for the response. Sorry, I should have clarified the models. I imagine an account document instance would be something like this:

{
  "type": "account",  
  "name": "Office Account", 
  "suppliers": {
    "supplierName": "Paper Supplier Inc",
    "supplierId": {{mongo _id}} 
  }
}

where the mongo _id is deeply nested inside some json. If you hoist the ref to a first level key, you get the example that you provided which is a nice work-around. I was just wondering whether this was possible since if you could, you could namespace and group refs

ainesophaur commented 9 years ago

I don't believe you can do directly what you're trying to. Are you working with existing data that is already in that format and you're trying to migrate to strongloop? If so you can just use a hasMany relation and set the foreignKey to supplierId. The name of the supplier won't be returned when you query the parent model unless you then query/lazy load the relation. On Oct 12, 2015 10:03 AM, "grindfuk" notifications@github.com wrote:

Thanks for the response. Sorry, I should have clarified the models. I imagine an account document instance would be something like this:

{ "type": "account", "name": "Office Account", "suppliers": { "supplierName": "Paper Supplier Inc", "supplierId": {{mongo _id}} } }

where the mongo _id is deeply nested inside some json. If you hoist the ref to a first level key, you get the example that you provided which is a nice work-around. I was just wondering whether this was possible since if you could, you could namespace and group refs

— Reply to this email directly or view it on GitHub https://github.com/strongloop/loopback-connector-mongodb/issues/180#issuecomment-147407829 .

grindfuk commented 9 years ago

Thanks. It is how the data is currently formatted but it is not hard to change on MongoDB. So, the foreign keys must be hoisted to the top level? Maybe something like:

{
  "type": "account",  
  "name": "Office Account", 
  "supplierId": {{mongo _id}},
  "supplierName": "Paper Supplier Inc",  
}
ainesophaur commented 9 years ago

No, the way you currently have the data would be fine. You would need to create a hasMany/belongsTo or hasAndBelongsToMany relation from the top level model to the supplier model. The relation name (or using the 'as' property) is what gets read from the top level keys.

Account model

{ properties: { type: String, name: String }, relations: { suppliers : { foreignKey: "supplierId", type : " hasAndBelongsToMany" } } }

If you can share the current schema for the two models I can let you know what to put in loopback.

I also need to know what's the relation between the models. Do Many suppliers belong to Many different accounts? Do you need a bidirectional relation where you can access each model in the relation from either side? On Oct 15, 2015 12:53 PM, "grindfuk" notifications@github.com wrote:

Thanks. It is how the data is currently formatted but it is not hard to change on MongoDB. So, the foreign keys must be hoisted to the top level? Maybe something like:

{ "type": "account", "name": "Office Account", "supplierId": {{mongo _id}}, "supplierName": "Paper Supplier Inc", }

— Reply to this email directly or view it on GitHub https://github.com/strongloop/loopback-connector-mongodb/issues/180#issuecomment-148454376 .

stale[bot] commented 7 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 7 years ago

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.