0

I have a schema in which i have an array Modules, in that array i want to get the element which has the same ModuleID as recieved in req.body.ModId, I get that element, and I want to set the ModuleStatement of that element and save it, but it doesn't save the ModuleStatement in database Here is the schema

var botSchema = new Schema({
Bot_name: String,
UserName:String,
Modules: [{
    ModuleStatement: String,
    ModuleID: String,
    ModuleResponse: [{
        Response: String,
        TransBotID: String
    }]
}]
});

following is the query to get the desired element from Modules array

botSchema.find({Bot_name:req.body.BotName},function(err,bot){
if(err){
    throw err;
} else{
    //update module here
    botSchema.find({'Modules.ModuleID':req.body.ModId},{_id:0,Modules:
    {$elemMatch:{ModuleID:req.body.ModId
    }}},function(err,data){
        if(err){
            throw err;
        } else{
            console.log('module array element');
            console.log(data[0]);
            data[0].ModuleStatement=req.body.Statement;
            console.log('module stateupdated',data[0].ModuleStatement);
            data[0].save();
            res.send('upgraded');
        }
    })
}
})

this returns the desired element of Modules array which matches the ModuleID with the ModId of req.body, however when i assign it the statement and save the parent document, the changes are not reflected in database. I thought may be the parent document can save the changed stated of element of array so i called the Save() function on the parent document like bot.save() but it didn't work either.How can i save it?

Ryan Gadsdon
  • 2,272
  • 4
  • 31
  • 56
Jawad
  • 313
  • 4
  • 16

0 Answers0