Update object in 3 rd level of nested array

hi guys i try update object in 3 rd level of nested array
this is the sample document

{
  "companyId": {"5ec1c4c7ea40dd3912ff206d"},
  "memberId": {"61e538180d543336dfa52a94"},
  "isActive": true,
  "name": "SISWA1",
  "profileImage": "string",
  "email": "1@siswa.com",
  "phone": "088123123123",
  "mainClassId": null,
  "classId": null,
  "datas": [
    {
      "id": {"6350ce45605a1c2f35e4c607"},
      "classId": {"62f19d68c149abd43526d1a3"},
      "lessonId": {"62f0af32cb716c443625c3d0"},
      "year": "2002",
      "report": [
        {
          "activityId": {"6350f1313f586971dfd1effd"},
          "meet": 3,
          "isPresent": true,
          "scores": [
            {
              "key": "TUGAS",
              "value": 50
            }
          ]
        },
        {
          "activityId": {"6350f13aaa8f2d84071fc3cd"},
          "meet": 4,
          "isPresent": true,
          "scores": [
            {
              "key": "TUGAS",
              "value": 50
            }
          ]
        }
      ]
    }
  ]
}

I’ve tried this way but there’s no change.
i want to update field meet in array report with id=“6350f13aaa8f2d84071fc3cd”, from 2 became 12

 student_activity_op = await USER_STUDENT.update_one(
            {"datas.report.activityId": ObjectId("6350f13aaa8f2d84071fc3cd")},
            {
                "$set": {
                    "datas.$[outer].report.$[inner].meet": 12
                }
            },
            {
                "$arrayFilters": [
                    {"outer.id": ObjectId("6350ce45605a1c2f35e4c607")},
                    {"inner.activityId": ObjectId("6350f13aaa8f2d84071fc3cd")}
                ]
            },
        )

if anyone can provide a solution i will appreciate

Hi @Nuur_zakki_Zamani ,

Are you certain that the values are actually objectids and not just strings?

In the print it looks like a string.

Thanks
Pavel

@Pavel_Duchovny
that’s just an example sir, originally every field in the document is objectid. I just don’t know how to update a meet field in the report array. can you help me sir?

Hi @Nuur_zakki_Zamani ,

Its a silly mistake, hope you going to laugh :slight_smile:

You have an unecessary dollar in your “$arrayFilters” expression:

{
                "$arrayFilters": [
...

It should be

{
                "arrayFilters": [
                    {"outer.id": ObjectId("6350ce45605a1c2f35e4c607")},
                    {"inner.activityId": ObjectId("6350f13aaa8f2d84071fc3cd")}
                ]
            }

When I changed that everything worked for me :slight_smile: You probably got an error that you missed.

The object changed for me as expected:

{ _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 } ] } ] } ] }

Thank
Pavel

1 Like

:rofl: :rofl: :rofl:yeayyyyy alhamdulillah.
finaly after trying for 3 days, i can sleep well.
thank you so much mr @Pavel_Duchovny. :saluting_face: :saluting_face:

1 Like

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