All documents mutated when only a single document modified

Given documents of this shape:

{
    "id": 1,
    "type": "mytype",
    "answers": [
        {
            "q": "q1",
            "v": "a1"
        }
    ]
}

and the following query:

UPDATE bucket SET a.v = 'a2' FOR a IN answers WHEN a.q = 'q1' AND a.v = 'a1' END
WHERE type='mytype'

The query console shows

1,000,000 mutations

When only a single document was modified.

Why?

How can you be sure only one document was modified?

We have 20k documents and it says it modified all of them, but only a single document was modified. Confirmed by running the following:

SELECT * FROM bucket WHERE type='mytype' AND ANY a IN answers SATISFIES a.v = 'a2' END

which returns a single document.

We’re scared of running the update query because the console is reporting that it’s mutating EVERY document.

I wonder if it is going by the number of documents matched in the WHERE. And if using the WHERE from your SELECT as the WHERE in the UPDATE would give 1 mutation.

WHERE type=‘mytype’

vs.

WHERE type=‘mytype’ AND ANY a IN answers SATISFIES a.v = ‘a2’ END

1 Like

good suggestion, thanks - I’ll get into it

You need to control by WHERE clause. Once WHERE condition is true the document must mutate even if not changed. conditions in SET/UNSET are more like case expression and mutate document even if it is same.

UPDATE bucket SET a.v = 'a2' FOR a IN answers WHEN a.q = 'q1' AND a.v = 'a1' END
WHERE type= "mytype" 
       AND ANY a IN answers SATISFIES a.q = "q1" AND a.v = "a1" END

The above one tells WHERE if any answer ARRAY has q = “q1” AND v = “v1” that document will qualify mutate. Then SET clause FOR clause tells which array element to modify.