蒙戈德集合的变化


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,这是文档中的一个对象数组,你有各种选择(快速的谷歌搜索将证明是有用的(。但是在我的头顶上,您可以尝试以下操作

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();

这只是为了让它运行。但是还有更好的选择,例如:

  • 如何在猫鼬中更新数组值
  • 数组的猫鼬更新

最新更新