Update array objects by condition

Matt_Dale
Tue, 22 Mar 2022 19:36:54 +0000

It seems like each element in an “arrayFilter” can only have a single top-level field name (see a similar error message in this StackOverflow question). Try updating your code to use one map key per array element.

For example:

...
	arrFilters := bson.A{}
	updateSet := bson.M{}
	for i, scanUuid := range scanUuids {
		idx := fmt.Sprintf("x%s", strconv.Itoa(i))
		arrFilters = append(arrFilters, bson.M{
			fmt.Sprintf("%s.scanUuid", idx): scanUuid,
		})
		updateSet[fmt.Sprintf("urls.$[%s].status", idx)] = status
	}

	arrayFilters := options.ArrayFilters{Filters: arrFilters}
	upsert := true
	opts := options.UpdateOptions{
		ArrayFilters: &arrayFilters,
		Upsert:       &upsert,
	}
...