1
votes

I have a Looopback model of arcade.json and another model is report.json. Now I want to give relation in report.json which contains an array of _id of arcade.json. How can I achieve this? I am using Loopback 3.x.

An array of id stored in string format but I want to stores into ObjectId (Which MongoDB stored) which as below:

{
"_id" : ObjectId("5afbd860069aaa06b79f09a8"),
"name" : "Report 1",
"arcadeIDList" : [
    {
        "arcadeid" : "5ae319e8ac5718155ca719d0"
    },
    {
        "arcadeid" : "5ae31a4d770afb158ef6c048"
    }
]}

But I want to stores arcadeid as an ObjectId not as a string as below:

   {
"_id" : ObjectId("5afbd860069aaa06b79f09a8"),
"name" : "Report 1",
"arcadeIDList" : [
    {
        "arcadeid" : ObjectId("5ae319e8ac5718155ca719d0")
    },
    {
        "arcadeid" : ObjectId("5ae31a4d770afb158ef6c048")
    }
]}

I did it with referencesMany relation but I can not able store arcadeid as an ObejctId and I don't know its perfect way or not.

I read the Github Issue #274 but I want to do it with the relation in Loopback Model.

Please do let me know If you know that how to give a relation of an array and how to define the array of _id in Loopback Model.

1

1 Answers

1
votes

Please check code that helps you. I have used an array of Ids in my projects.

"userIds" : [ 
        ObjectId("5876721bc44fb9e02114d2dd"), 
        ObjectId("58767453c44fb9e02114d2e0")
    ],

Relation in table with User

"properties": {
"userIds" : {
          "type" : "array"
      }
  },
 "relations": {
      "users": {
          "type": "referencesMany",
          "model": "User",
          "foreignKey": "userIds"
      }
    },