mongodb - 如何从关系中获取数据以及存储在 Id 数组中的数据
问题描述
我有一个模型批次,其数据为
"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);
})
但我没有得到任何价值,任何人都可以帮助通过关系获得价值
谢谢你!
解决方案
我改进了你的代码。请试试这个
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);
})
推荐阅读
- linux-kernel - Gentoo 没有规则来制作目标'olddefconfig'
- java - 如何删除 Cloudinary Url 中的版本标签?
- laravel - Laravel 雄辩的分页关系未显示在集合中
- macos - 使用 jar xvf 提取目录中的所有档案?macOS 终端
- android - 完成活动不会重置它
- android - 如何在导航抽屉顶部创建工具栏(Android/kotlin)
- webpack - 为什么当我使用 Vue 的运行时版本时,我的 Vue.js 应用程序无法运行?
- ios - UITableview 单元格切断了一些消息(视图)
- java - 如何在 Testng 中查找 Test suite.xml 执行状态
- sql - BigQuery:从数组创建表