node.js - MongoDB 集合中的更改
问题描述
const appliedBySchema = new mongoose.Schema({
_id: { type: mongoose.Schema.Types.ObjectId, ref: "user" },
timestamp: { type: Date, default: new Date() },
status: { type: String, default: "Pending" }
});
const positionSchema = new mongoose.Schema({
job_id: { type: mongoose.Schema.Types.ObjectId,ref:"ad"},
postedBy: { type: mongoose.Schema.Types.ObjectId,ref:"user" },
positionName: String,
reqExp: String,
appliedBy: [appliedBySchema],
dept:{type:String , default: false},
mainRole:{type: String, default: false},
genderStrictly:{type: String, default:false},
ageStrictly:{type: String, default:false}
});
我想更改状态appliedBySchema
,请告诉我我必须编写的 mongo 查询以更新状态。现在的模型,但不存在positionSchema
的模型。appliedBySchema
解决方案
如果您要更新appliedBySchema
,它是文档中的对象数组,您有多种选择(快速的 Google 搜索将证明有用)。但是在我的脑海中,你可以尝试以下
const Position = mongoose.model('positions', positionSchema);
const toUpdate = await Position.findById(idToUse, 'appliedBy');
const appliedBy = toUpdate.appliedBy || []; // I now have access to all the applied by data
toUpdate.appliedBy = appliedBy.map(entity => {
// Make manipulations on the entity
return entity;
});
await appliedBy.save();
这只是为了让它运行。但是那里有更好的选择,例如:
推荐阅读
- apache-spark - 如何在 SparkMetrics 中按命名空间过滤输出中的指标?
- python - __str__ 已经返回 unicode 时出现 UnicodeEncodeError
- angular - Angular 5 以自定义格式导出 Excel
- python - 如何用熊猫按顺序标记编码多个类别(多行)?
- typescript - 如何定义“实现”接口的类型?
- java - Hikari CP 检测到明显的连接泄漏
- ios - iOS11出现键盘时UITableView将键盘高度添加到contentSize
- javascript - 基于光标位置的平移和缩放(在鼠标滚轮上)
- sql - Postgres SQL 在最近不到季度结束时加入
- java - 如何从 GWT 应用程序调用 J2ee 应用程序的方法?