How to delete object in nested array array

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

Hello @Nuur_zakki_Zamani,

The condition for inner in arrayFilters is not required, you can put that condition inside the $pull operation,

db.collection.update({
  "_id": ObjectId("6352b213f60e6c14eade7dc5"),
  "datas": {
    "$elemMatch": {
      "id": ObjectId("6350ce45605a1c2f35e4c607"),
      "report.activityId": ObjectId("6350f1313f586971dfd1effd")
    }
  }
},
{
  $pull: {
    "datas.$[outer].report": {
      "activityId": ObjectId("6350f1313f586971dfd1effd")
    }
  }
},
{
  arrayFilters: [
    { "outer.id": ObjectId("6350ce45605a1c2f35e4c607") }
  ]
})

Playground

1 Like

Hi @Nuur_zakki_Zamani and @turivishal ,

There is actually a simple way to write it:

db.collection.updateOne({ "datas": {
    "$elemMatch": {
      "id": ObjectId("6350ce45605a1c2f35e4c607"),
      "report.activityId": ObjectId("6350f1313f586971dfd1effd")
    }
  }},{$pull : { "datas.$.report" : { activityId:  ObjectId("6350f13aaa8f2d84071fc3cd")} }})

The positional $ is working fine if there is only one occurrence of activityId: ObjectId("6350f13aaa8f2d84071fc3cd"

Thanks
Pavel

3 Likes

yesss thanks mr @Pavel_Duchovny it work in my python :saluting_face: :saluting_face:

1 Like

thanks bro @turivishal :saluting_face:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.