I am struggling to find the format for a query to remove an element (with an _id) from an array of arrays in Mongo via Mongoose. I have read the Mongo documentation on pull and searched around, but cannot get the query format correct.
I know: the _id of the document in MySchema, the _id of the array element in outerArray, and the _id of the the array element in innerArray.
Could someone please point out what I am doing wrong? Thank you!
Mongo Schema:
MySchema {
outerArray: [ {
field1 : String,
innerArray [ {
field2: String
} ]
} ]
}
Mongoose Query - This does not work:
MySchema.findByIdAndUpdate(
mongoose.Types.ObjectId(docId),
{
$pull: {
'outerArray': {
$elemMatch: {
'_id': mongoose.Types.ObjectId(outerArrayDocId),
'innerArray': {
'_id': mongoose.Types.ObjectId(innerArrayDocId)
}
}
}
}
},
(err : mongoose.Error, updated : any) => {
});