Using multiple arrayFilters on the same top-level field in MongoDB with Go

I’m trying to update a nested document in MongoDB based on certain conditions. The problem is that using arrayFilters on the same top-level field name is not allowed. Any solutions for solving this problem?

Error: Found multiple array filters with the same top-level field name x

	filter := bson.D{primitive.E{Key: "_id", Value: e.EventId}}

	arrayFilters := options.ArrayFilters{
		Filters: []interface{}{
			bson.M{"x._id": tt.TypeId},
			bson.M{"x.typeAmountUsed": bson.M{"$lt": "$x.typeAmount"}}, // <-- multiple filters
		},
	}

	upsert := true
	opts := options.UpdateOptions{
		ArrayFilters: &arrayFilters,
		Upsert:       &upsert,
	}
	update := bson.M{
		"$inc": bson.D{{"eventTicketTypes.$[x].typeAmountUsed", 1}},
	}

    if _, err = events.UpdateOne(sessCtx, filter, update, &opts); err != nil {
		return nil, err
	}

Solved by doing:

	arrayFilters := options.ArrayFilters{
		Filters: []interface{}{
			bson.D{
				{"x._id", tt.TypeId},
				{"x.typeAmountUsed", bson.M{"$lt": "$x.typeAmount"}},
			},
		},
	}

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