Spring AggregationUpdate multiple array elements

Viewed 150

I'm trying to update many objects within a nested array when their IDs exist in a list.

This is the document structure

{
    id: p00,
    children: [{
            id: c00,
            value: false
        },
        {
            id: c01,
            value: false
        },
        {
            id: c02,
            value: false
        }
    ]
}

I would like to update all array elements value=true if id=p00 and children.id in [c00, c01].

Using a MatchOperation to find id=p00, and passing to AggregationUpdate, I am not able to even update all array elements.

MatchOperation matchStage = Aggregation.match(
    new Criteria("id").is("p00"));
        
    AggregationUpdate update = AggregationUpdate.newUpdate(matchStage)
        .set("children.$.value").toValue(true);
        
    template.update(Parent.class)
        .apply(update)
        .all();

Exception thrown:

org.springframework.dao.DataIntegrityViolationException: Invalid $set :: caused by :: FieldPath field names may not be empty strings.; nested exception is com.mongodb.MongoWriteException: Invalid $set :: caused by :: FieldPath field names may not be empty strings.

What could be wrong? The $ syntax should return for the nest array elements.

0 Answers
Related