最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

mongodb集合的变化

运维笔记admin16浏览0评论

mongodb集合的变化

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

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

  • How to update an array value in Mongoose
  • Mongoose update of array

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论