-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check that different update operators do not target same field (conflict) #232
Comments
An additional caveat is that an update operator cannot target subpaths of a path that another update operator is targetting. For example, the following throws a
|
@vkarpov15 yes, I was worried this comes up. Straight up comparison is easier than prefixes. :) But... well, I'll see whether it makes sense to first cover simple(r) case & then do follow-up, or if I just want to tackle the full case right on. Implementation-wise latter can be done, I think, by sorting the path & comparing adjacent paths to see if first one is a parent-prefix of the second one (suffix starting with |
What we often end up doing in Mongoose is creating a const updatedPaths = new Set();
for (const key of Object.keys(update)) {
if (key.indexOf('.') === -1) {
updatedPaths.add(key);
continue;
}
const pieces = key.split('.');
let cur = pieces[0];
updatedPaths.add(cur);
for (let i = 1; i < pieces.length; ++i) {
cur += '.' + pieces[i];
updatedPaths.add(cur);
}
} Add a check for duplicates and you're done. Should work in this case, unless you think it is too slow. |
Thanks @vkarpov15! Something along these lines would work, yes. Will tackle this after doing some refactoring to unify path handling. |
Although I have not seen this mentioned in general (but will see if I can find), update operators can NOT target same field in same operation. There is an explicit mention of "$set" and "$unset" not allowing this but it appears general limitation; for example trying:
gives error
possibly because order in which operators are executed does not appear to be strongly defined (unlike per-field ordering that is).
We should similarly cross-check paths across different operators (within operators JSON does not allow duplicates) and fail update upon found conflict.
The text was updated successfully, but these errors were encountered: