Update nested arrays with upsert

Hello

The bellow does
1)Add new book
if no books array,or new category
2)else new member on add bookinfo

*uses pipeline update so need mongodb >=4.2

Change those to the query below,use variables
newBook needs to have this structure for code to work,else change the code also

NewBook

 "newBook": {
                    "Name": "xxx2",
                    "Auth": "xxx2",
                    "Category": 2
                  }

UserID

"q": {
        "UserId": 1
      }

What it does
if books doesnt exists adds books have only the newbooks
else
reduce on the books array,to updated books-array
if category exists
add to bookinfo
else if i found the end and i dindt added yet(new category)
add new-book to books
It does it with one array read

{
  "update": "testcoll",
  "updates": [
    {
      "q": {
        "UserId": 1
      },
      "u": [
        {
          "$addFields": {
            "Books": {
              "$let": {
                "vars": {
                  "newBook": {
                    "Name": "xxx2",
                    "Auth": "xxx2",
                    "Category": 2
                  }
                },
                "in": {
                  "$cond": [
                    {
                      "$not": [
                        {
                          "$ne": [
                            {
                              "$type": "$Books"
                            },
                            "missing"
                          ]
                        }
                      ]
                    },
                    [
                      {
                        "Category": "$$newBook.Category",
                        "BookInfos": [
                          {
                            "Name": "$$newBook.Name",
                            "Auth": "$$newBook.Auth"
                          }
                        ]
                      }
                    ],
                    {
                      "$arrayElemAt": [
                        {
                          "$reduce": {
                            "input": "$Books",
                            "initialValue": [
                              [],
                              0,
                              false
                            ],
                            "in": {
                              "$let": {
                                "vars": {
                                  "booksIndexAdded": "$$value",
                                  "book": "$$this"
                                },
                                "in": {
                                  "$let": {
                                    "vars": {
                                      "books": {
                                        "$arrayElemAt": [
                                          "$$booksIndexAdded",
                                          0
                                        ]
                                      },
                                      "index": {
                                        "$arrayElemAt": [
                                          "$$booksIndexAdded",
                                          1
                                        ]
                                      },
                                      "added": {
                                        "$arrayElemAt": [
                                          "$$booksIndexAdded",
                                          2
                                        ]
                                      }
                                    },
                                    "in": {
                                      "$cond": [
                                        {
                                          "$eq": [
                                            "$$book.Category",
                                            "$$newBook.Category"
                                          ]
                                        },
                                        [
                                          {
                                            "$concatArrays": [
                                              "$$books",
                                              [
                                                {
                                                  "$mergeObjects": [
                                                    "$$book",
                                                    {
                                                      "BookInfos": {
                                                        "$concatArrays": [
                                                          "$$book.BookInfos",
                                                          [
                                                            {
                                                              "Name": "$$newBook.Name",
                                                              "Auth": "$$newBook.Auth"
                                                            }
                                                          ]
                                                        ]
                                                      }
                                                    }
                                                  ]
                                                }
                                              ]
                                            ]
                                          },
                                          {
                                            "$add": [
                                              "$$index",
                                              1
                                            ]
                                          },
                                          true
                                        ],
                                        {
                                          "$cond": [
                                            {
                                              "$and": [
                                                {
                                                  "$eq": [
                                                    "$$index",
                                                    {
                                                      "$subtract": [
                                                        {
                                                          "$size": "$Books"
                                                        },
                                                        1
                                                      ]
                                                    }
                                                  ]
                                                },
                                                {
                                                  "$not": [
                                                    "$$added"
                                                  ]
                                                }
                                              ]
                                            },
                                            [
                                              {
                                                "$concatArrays": [
                                                  "$$books",
                                                  [
                                                    "$$book",
                                                    {
                                                      "Category": "$$newBook.Category",
                                                      "BookInfos": [
                                                        {
                                                          "Name": "$$newBook.Name",
                                                          "Auth": "$$newBook.Auth"
                                                        }
                                                      ]
                                                    }
                                                  ]
                                                ]
                                              },
                                              {
                                                "$add": [
                                                  "$$index",
                                                  1
                                                ]
                                              },
                                              true
                                            ],
                                            [
                                              {
                                                "$concatArrays": [
                                                  "$$books",
                                                  [
                                                    "$$book"
                                                  ]
                                                ]
                                              },
                                              {
                                                "$add": [
                                                  "$$index",
                                                  1
                                                ]
                                              },
                                              "$$added"
                                            ]
                                          ]
                                        }
                                      ]
                                    }
                                  }
                                }
                              }
                            }
                          }
                        },
                        0
                      ]
                    }
                  ]
                }
              }
            }
          }
        }
      ],
      "upsert": true,
      "multi": true
    }
  ]
}

Before update
Screenshot from 2020-12-25 14-51-32

New user
NewUser

New category old user
NewCategoryOlduser

Old category old user
OldCategoryOldUser