1
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}
    });

I want to change the status in appliedBySchema, please tell me the mongo query that I have to write to update the status. The model of positionSchema present but the model of appliedBySchema does not exist.

juanlumn
  • 6,155
  • 2
  • 30
  • 39

1 Answers1

0

If you're looking to update the appliedBySchema, which is an array of objects inside a document, you have various options (which a quick Google search will prove useful for). But off the top of my head, you can try the following

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

This is just to get it running. But there are far better options out there, such as:

cross19xx
  • 3,170
  • 1
  • 25
  • 40