skip to Main Content

I have a collection containing documents as such:

  {
    "category":"A",
    "list": [
      {
        "item": 1,
        "sub_list": [ 11 ]
      },
      {
        "item": 2,
        "sub_list": [13, 43]
      },
    ],
  }

How do I add a number to the sub_list given category & item? and if it doesn’t exist then upsert and create

db.collection.update({
  "category": "A", // "B" will create/upsert new document
  "list.item": 1 // 2 will add new obj to list
},
 ???
{
  upsert: true
})

2

Answers


  1. Tried to work with a solution.

    Got something similar as Umer Abbas comment.

    db.collection.update({
      "category": "A"
    },
    [
      {
        "$set": {
          "list": {
            "$cond": {
              if: {
                $in: [
                  1,
                  {
                    "$ifNull": [
                      "$list.item",
                      []
                    ]
                  }
                ]
              },
              then: {
                $map: {
                  input: "$list",
                  in: {
                    $cond: {
                      if: {
                        $and: [
                          {
                            $eq: [
                              "$$this.item",
                              1
                            ]
                          },
                          {
                            $not: {
                              $in: [
                                11,
                                {
                                  "$ifNull": [
                                    "$$this.sub_list",
                                    []
                                  ]
                                }
                              ]
                            }
                          }
                        ]
                      },
                      then: {
                        $mergeObjects: [
                          "$$this",
                          {
                            "sub_list": {
                              "$concatArrays": [
                                {
                                  "$ifNull": [
                                    "$$this.sub_list",
                                    []
                                  ]
                                },
                                [
                                  11
                                ]
                              ]
                            }
                          }
                        ]
                      },
                      else: "$$this"
                    }
                  }
                }
              },
              else: {
                "$concatArrays": [
                  [
                    {
                      "item": 1,
                      "sub_list": [
                        11
                      ]
                    }
                  ],
                  {
                    $ifNull: [
                      "$list",
                      []
                    ]
                  }
                ]
              }
            }
          }
        }
      }
    ],
    {
      "upsert": true
    })
    

    You can try it on mongoplayground.


    EDITED ANSWER

    const categoryVal = "A"
    const itemToFind = 1
    const valToAdd = 68
    db.collection.update({
      "category": categoryVal
    },
    [
      {
        "$set": {
          "list": {
            "$cond": {
              if: {
                $in: [
                  itemToFind,
                  {
                    "$ifNull": [
                      "$list.item",
                      []
                    ]
                  }
                ]
              },
              then: {
                $map: {
                  input: "$list",
                  in: {
                    $cond: {
                      if: {
                        $and: [
                          {
                            $eq: [
                              "$$this.item",
                              itemToFind
                            ]
                          },
                          {
                            $not: {
                              $in: [
                                valToAdd,
                                {
                                  "$ifNull": [
                                    "$$this.sub_list",
                                    []
                                  ]
                                }
                              ]
                            }
                          }
                        ]
                      },
                      then: {
                        $mergeObjects: [
                          "$$this",
                          {
                            "sub_list": {
                              "$concatArrays": [
                                {
                                  "$ifNull": [
                                    "$$this.sub_list",
                                    []
                                  ]
                                },
                                [
                                  valToAdd
                                ]
                              ]
                            }
                          }
                        ]
                      },
                      else: "$$this"
                    }
                  }
                }
              },
              else: {
                "$concatArrays": [
                  [
                    {
                      "item": itemToFind,
                      "sub_list": [
                        valToAdd
                      ]
                    }
                  ],
                  {
                    $ifNull: [
                      "$list",
                      []
                    ]
                  }
                ]
              }
            }
          }
        }
      }
    ],
    {
      "upsert": true
    })
    
    Login or Signup to reply.
  2. To push a number to the sub_list, we require to do the following:

    db.collection.update(
      {
        "category": "A", 
        "list.item": 1   
      },
      {
        "$push": {"list.$.sub_list": 4} // 4 will be added to sub_list
      },
      {
        upsert: true
      }
    );
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search