Mongodb 如何从关系';存储在数组中的数据';身份证号码
我有一个模型,它的数据如下Mongodb 如何从关系';存储在数组中的数据';身份证号码,mongodb,foreign-keys,relationship,loopback,Mongodb,Foreign Keys,Relationship,Loopback,我有一个模型,它的数据如下 "id": { "type": "string", "id": true, "defaultFn": "uuidv4" }, "batchType": { "type": "string", "required": true, "default": "COURSE" }, "venue": { "type": "string", "requi
"id": {
"type": "string",
"id": true,
"defaultFn": "uuidv4"
},
"batchType": {
"type": "string",
"required": true,
"default": "COURSE"
},
"venue": {
"type": "string",
"required": true
},
还有一个模特说预订了浴室
"batchesId": {
"type": [
"string"
],
"required": true
},
并创建了从批次模型到BookedBatchs模型的关系,如下所示:
"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);
})
但是我没有得到任何价值,任何人都可以通过关系来帮助获得价值吗
谢谢大家! 我已经改进了你的代码。请试试这个
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);
})