How can you only update one match of an array filter

Problem:

I need to only update one document in the spots available array that has an id of “empty”. My previous query was updating all matching sub documents with “empty” as the id; which is no good Example Below. So I decided to use aggregation so that I could add a limit stage so that I could only update one item, but come to find out I cannot update the original document with an aggregation. This leaves the only option to use an array filter that only updates one/first of its matches. Is this possible? I feel like there has to be a way to only update one match on an array filter and if there isn’t this is definitely something that should be added.

My code:

This code updates every object with “empty”

        const client = await clientPromise;
            const db = client.db();
            // const query = db.collection('events').aggregate(agg);


            const query = await db.collection('events').updateOne({
                _id: new ObjectId("6398c34ca67dbe3286452f23"),
                createdBy: new ObjectId("636c1778f1d09191074f9690"),
                "weights.weight": 12
            },
                {
                    $set: {
                        "weights.$.spotsAvailable.$[el2]": {
                            "name": "Wayne Wrestler",
                            "userId": new ObjectId("636c1778f1d09191074f9690")
                        }
                    }
                },
                {
                    arrayFilters: [{ "el2": { "userId": "empty" } }]
                })

Example documents:

Event:

{
  "_id": {
    "$oid": "6398c34ca67dbe3286452f23"
  },
  "name": "test",
  "createdBy": {
    "$oid": "636c1778f1d09191074f9690"
  },
  "description": "testing",
  "date": {
    "$date": {
      "$numberLong": "1645488000000"
    }
  },
  "location": {
    "type": "Point",
    "coordinates": [
      0,
      0
    ]
  },
  "weights": [
    {
      "spotsAvailable": [
        {
          "name": "empty",
          "userId": "empty"
        },
        {
          "name": "empty",
          "userId": "empty"
        },
        {
          "name": "empty",
          "userId": "empty"
        }
      ],
      "weight": 12
    },
    {
      "spotsAvailable": [
        {
// only one of these should've been updated, but both were
          "name": "Wayne Wrestler",
          "userId": {
            "$oid": "636c1778f1d09191074f9690"
          }
        },
        {
          "name": "Wayne Wrestler",
          "userId": {
            "$oid": "636c1778f1d09191074f9690"
          }
        }
      ],
      "weight": 15
    }
  ],
  "eventApplicants": [
    {
      "userId": {
        "$oid": "636c1778f1d09191074f9690"
      },
      "name": "Wayne Wrestler",
      "weight": 12
    }
  ]
}

User:

{
  "_id": {
    "$oid": "636c1778f1d09191074f9690"
  },
  "name": "Wayne Wrestler",
  "email": "wakywayne80@gmail.com",
  "image": "https://lh3.googleusercontent.com/a/ALm5wu32gXjDIRxncjjQA9I4Yl-sjFH5EWsTlmvdM_0kiw=s96-c",
  "emailVerified": {
    "$date": {
      "$numberLong": "1670864727212"
    }
  },
  "createdEvents": [
    {
      "createdEventName": "test",
      "createdEventDate": {
        "$date": {
          "$numberLong": "1645488000000"
        }
      },
      "createdEventDescription": "testing",
      "createdEventWeights": [
        {
          "weight": "12",
          "filled": [
            false,
            false,
            false
          ]
        },
        {
          "weight": "15",
          "filled": [
            false,
            false
          ]
        }
      ],
      "createdEventId": {
        "$oid": "6398c34ca67dbe3286452f23"
      }
    }
  ],
  "userSignedUpEvents": [],
  "availableWeights": [
    1,
    123
  ],
  "signedUpEvents": [
    {
      "eventId": {
        "$oid": "636c722f67642c30dc5ffc30"
      },
      "eventName": "Utah",
      "eventDate": {
        "$date": {
          "$numberLong": "1667913330000"
        }
      },
      "accepted": false
    },
    {
      "eventId": {
        "$oid": "636c722f67642c30dc5ffc30"
      },
      "eventName": "Utah",
      "eventDate": {
        "$date": {
          "$numberLong": "1667913330000"
        }
      },
      "accepted": false
    },
    {
      "eventId": {
        "$oid": "637ec484ac2d675b30590b47"
      },
      "eventName": "Maybe?",
      "eventDate": {
        "$date": {
          "$numberLong": "1672272000000"
        }
      },
      "accepted": false
    },
    {
      "eventId": {
        "$oid": "636c722f67642c30dc5ffc30"
      },
      "eventName": "Utah",
      "eventDate": {
        "$date": {
          "$numberLong": "1667913330000"
        }
      },
      "accepted": false
    },
    {
      "eventId": {
        "$oid": "638d5274628db2a7bf61df49"
      },
      "eventName": "Eva's",
      "eventDate": {
        "$date": {
          "$numberLong": "1698019200000"
        }
      },
      "accepted": false
    },
    {
      "eventId": {
        "$oid": "636c722f67642c30dc5ffc30"
      },
      "eventName": "Utah",
      "eventDate": {
        "$date": {
          "$numberLong": "1667913330000"
        }
      },
      "accepted": false
    },
    {
      "eventId": {
        "$oid": "6398a922abb5c168ede595fb"
      },
      "eventName": "Nikko's event",
      "eventDate": {
        "$date": {
          "$numberLong": "1670976000000"
        }
      },
      "accepted": false
    },
    {
      "eventId": {
        "$oid": "6398a922abb5c168ede595fb"
      },
      "eventName": "Nikko's event",
      "eventDate": {
        "$date": {
          "$numberLong": "1670976000000"
        }
      },
      "accepted": false
    },
    {
      "eventId": {
        "$oid": "6398c34ca67dbe3286452f23"
      },
      "eventName": "test",
      "eventDate": {
        "$date": {
          "$numberLong": "1645488000000"
        }
      },
      "accepted": false
    },
    {
      "eventId": {
        "$oid": "6398c34ca67dbe3286452f23"
      },
      "eventName": "test",
      "eventDate": {
        "$date": {
          "$numberLong": "1645488000000"
        }
      },
      "accepted": false
    },
    {
      "eventId": {
        "$oid": "6398c34ca67dbe3286452f23"
      },
      "eventName": "test",
      "eventDate": {
        "$date": {
          "$numberLong": "1645488000000"
        }
      },
      "accepted": false
    },
    {
      "eventId": {
        "$oid": "6398c34ca67dbe3286452f23"
      },
      "eventName": "test",
      "eventDate": {
        "$date": {
          "$numberLong": "1645488000000"
        }
      },
      "accepted": false
    }
  ]
}

I have tried:

  1. Pluging in variables without the new ObjectId syntax
  2. Plugin in variables with the new ObjectId syntax
  3. Using the exact same hardcoded values that I got from copying the aggregation code from compass for the node driver

All of these either don’t work or result in every subdocument with “empty” getting filled

To help the discussion I have linked your other thread as it looks like it is exactly the same question.

I have found a solution:

db.collection.update(
  {_id: ObjectId("6398c34ca67dbe3286452f23"), "weights.weight": 12},
  [
    {$set: {
      newItem: {$reduce: {
          input: {$getField: {
              input: {$first: {
                  $filter: {
                    input: "$weights",
                    as: "item",
                    cond: {$eq: ["$$item.weight", 12]}
                  }
              }},
              field: "spotsAvailable"
          }},
          initialValue: [],
          in: {$concatArrays: [
              "$$value",
              {$cond: [
                  {$and: [
                      {$eq: ["$$this.userId", "empty"]},
                      {$not: {$in: [ObjectId("636c1778f1d09191074f9690"), "$$value.userId"]}}
                  ]},
                  [{
                      name: "Wayne Wrestler",
                      userId: ObjectId("636c1778f1d09191074f9690")
                  }],
                  ["$$this"]
                ]}
            ]}
      }}
  }},
  {$set: {
      weights: {$map: {
          input: "$weights",
          in: {$cond: [
              {$eq: ["$$this.weight", 12]},
              {$mergeObjects: [
                  "$$this",
                  {spotsAvailable: "$newItem"}
              ]},
              "$$this"
          ]}
      }},
      newItem: "$$REMOVE"
  }}
])
1 Like

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