node.js - 使用其他字段填充文档
问题描述
我得到以下文件:
{
"_id": "5b5df332879b4769739e3ed0",
"name": "Back workout",
"exercises": [{
"_id": "5b5df9a7879b4769739e3ed1",
"results": [{"reps":"10","weight":"85"}, {"reps":"8","weight":"70"}]
}]
}
我想根据数据填充练习_id
并附加results
数据。
预期输出:
{
"_id": "5b5df332879b4769739e3ed0",
"name": "Back workout",
"exercises": [{
"_id": "5b5df9a7879b4769739e3ed1",
"name": "EXERCISE_NAME",
"results": [{"reps":"10","weight":"85"}, {"reps":"8","weight":"70"}]
}]
}
我的架构如下所示:
const WorkoutSchema = new Schema({
_id: Schema.Types.ObjectId,
name: String,
exercises: [{
_id: { type: Schema.Types.ObjectId, ref: 'Exercise' },
results: Array,
}],
createdAt: Date,
updatedAt: Date,
});
我的查询如下所示:
exports.listAll = async (request, h) => {
const workoutCollection = await Workout
.find()
.populate('exercise');
return { data: workoutCollection };
};
解决方案
推荐阅读
- hyperledger-fabric - 如何使用教程加入 swarm,wahab 的 Hyperledger Fabric on multiple hosts
- kubernetes - 在 kube-apiserver 中注册一个 kubelet
- dialogflow-es - Dialogflow - 在捕获所需参数(实体)时触发其他意图
- oracle - 删除语句批处理中是否存在操作员?
- pandas - 在 Google Colab 上使用 AddMoleculeColumnToFrame 时出现问题
- python - 有没有办法解压缩 GZIP 数据,然后将其返回字节流,然后返回首选编码
- swift - 保存密码警报不会出现在 Swift 中
- c - 如何编写一个函数来检查两个数组是否水平对称?
- laravel - 调用未定义函数 App\Http\Middleware\str_plural()
- android - 制作日历;更改年份,当月份大于 12 月或小于 1 月时