hi guy’s i try to delete some document in nested array with $pull operator
this is the sample document
[
{
_id: ObjectId("6352b213f60e6c14eade7dc5"),
companyId: ObjectId("5ec1c4c7ea40dd3912ff206d"),
memberId: ObjectId("61e538180d543336dfa52a94"),
isActive: true,
name: "SISWA1",
profileImage: "string",
email: "1@siswa.com",
phone: "088123123123",
mainClassId: null,
classId: null,
datas: [
{
id: ObjectId("6350ce45605a1c2f35e4c607"),
classId: ObjectId("62f19d68c149abd43526d1a3"),
lessonId: ObjectId("62f0af32cb716c443625c3d0"),
year: "2002",
report: [
{
activityId: ObjectId("6350f1313f586971dfd1effd"),
meet: 3,
isPresent: true,
scores: [
{
key: "TUGAS",
value: 50
}
]
},
{
activityId: ObjectId("6350f13aaa8f2d84071fc3cd"),
meet: 12,
isPresent: true,
scores: [
{
key: "TUGAS",
value: 50
}
]
}
]
}
]
}
]
i want to delete document that have activity=“6350f13aaa8f2d84071fc3cd”, so the document became like this
[
{
_id: ObjectId("6352b213f60e6c14eade7dc5"),
companyId: ObjectId("5ec1c4c7ea40dd3912ff206d"),
memberId: ObjectId("61e538180d543336dfa52a94"),
isActive: true,
name: "SISWA1",
profileImage: "string",
email: "1@siswa.com",
phone: "088123123123",
mainClassId: null,
classId: null,
datas: [
{
id: ObjectId("6350ce45605a1c2f35e4c607"),
classId: ObjectId("62f19d68c149abd43526d1a3"),
lessonId: ObjectId("62f0af32cb716c443625c3d0"),
year: "2002",
report: [
{
activityId: ObjectId("6350f1313f586971dfd1effd"),
meet: 3,
isPresent: true,
scores: [
{
key: "TUGAS",
value: 50
}
]
}
]
}
]
}
]
i’ve been try with this way, but doesnt work.
it even pops up a message
fail to run update: write exception: write errors: [Cannot apply $pull to a non-array value]
db.collection.update({
"_id": ObjectId("6352b213f60e6c14eade7dc5"),
"datas": {
"$elemMatch": {
"id": ObjectId("6350ce45605a1c2f35e4c607"),
"report.activityId": ObjectId("6350f1313f586971dfd1effd")
}
},
},
{
"$pull": {
"datas.$[outer].report.$[inner].activityId": ObjectId("6350f13aaa8f2d84071fc3cd")
}
},
{
"arrayFilters": [
{
"outer.id": ObjectId("6350ce45605a1c2f35e4c607")
},
{
"inner.activityId": ObjectId("6350f13aaa8f2d84071fc3cd")
}
]
},
)
sample code : Mongo playground