I have been trying to push an object in a nested array using mongoose update(), $elemMatch and $arrayfilters.
Here is the schema:
var details = new Schema({
var1: { type: String, required: true },
array1: [{
var2: { type: String, required: true },
array2: [{
var3: { type: String, required: true },
array3: [{
a: { type: String, required: true },
b: { type: Number, required: true },
c: { type: Date, required: true }
}]
}]
}]
})
Here is the mongoose code I have written where I want to push the object in array3:
Details.update(
{
var1: value1,
array1: {
"$elemMatch": {
var2: value2,
"array2.var3": value3
}
}
},
{
"$push": {
"array1.$[outer].array2.$[inner].array3": {
a: valueOfa,
b: valueOfb,
c: valueOfc
}
}
},
{
"arrayFilters": [{ "outer": value2}, { "inner": value3}]
}, function (err, data) {
if (err) {
console.log('Error!', err);
response.sendStatus(500)
} else {
console.log('pushed')
response.sendStatus(200)
}
}
)
I am getting no errors! I am getting the status as 200. But the object is not getting pushed in array3. Need some help here.