如何从关联的数据中获取数据以及存储在ID数组中的数据

时间:2019-10-10 11:24:16

标签: mongodb foreign-keys relationship loopback

我有一个模型批次,其数据为

"id": {
      "type": "string",
      "id": true,
      "defaultFn": "uuidv4"
    },
    "batchType": {
      "type": "string",
      "required": true,
      "default": "COURSE"
    },
    "venue": {
      "type": "string",
      "required": true
    },

和另一位预订的模特说预订了

"batchesId": {
     "type": [
       "string"
     ],
     "required": true
   },

并创建了一个从批次模型到bookedBatches模型的关系

 "relations": {
    "bookedBatches": {
      "type": "referencesMany",
      "model": "bookedBatches",
      "foreignKey": "batchesId",
      "primaryKey": "id"
    }
  }

现在我希望所有具有预订详细信息的批次都存储为批次模型中预订详细信息中ID数组的形式


let reqObject = {
                    "where": {
                        "and": [{
                            userId: userId,
                            isActive: true
                        }]
                    },
                    "order": "createdAt DESC",
                    include: [
                        {{
                            relation:"bookedBatches"}}]
}


Batches.find(reqObject, (resError, resData) => {
                    if (resError) {
                        return cb(new HttpErrors.BadRequest(resError, {
                            expose: false
                        }))
                    }
                    return cb(null, resData);
                })

但是我没有任何价值可以通过关系的

获得任何价值

谢谢!

1 个答案:

答案 0 :(得分:0)

我已经改善了您的代码。请尝试

 let reqObject = {
  "where": {
    "and": [{
      userId: userId,
      isActive: true
    }]
  },
  "order": "createdAt DESC",
  include: [
    {
      relation: "bookedBatches",
      scope: { fields: ["id","batchesId"] }
    }
  ]
}


Batches.find(reqObject, (resError, resData) => {
  if (resError) {
    return cb(new HttpErrors.BadRequest(resError, {
      expose: false
    }))
  }
  return cb(null, resData);
})