【问题标题】:Change in the mongodb collectionMongoDB 集合中的更改
【发布时间】:2019-07-06 23:45:44
【问题描述】:
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 的模型不存在。

【问题讨论】:

  • The model of positionSchema present but the model of appliedBySchema does not exist.是什么意思?
  • @ŞivāSankĂr 我想他想说的是他在位置架构上运行了mongoose.model(),但没有在appliedBySchema上运行]
  • 正确,@cr05s19xx。

标签: node.js mongodb mongoose mongoose-schema


【解决方案1】:

如果您要更新 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();

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多