[SERVER-59132] Improve $merge failure when matched error message Created: 05/Aug/21 Updated: 29/Oct/23 Resolved: 22/Nov/21 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | 5.2.0 |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Alice Thum | Assignee: | Hana Pearlman |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | neweng, quick-tech-debt | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Backwards Compatibility: | Fully Compatible |
| Participants: |
| Description |
|
When executing a $merge with a whenMatched: fail option specified, the error message can be misleading. See the following example:
Instead of elucidating that the operation failed because a document in the source matched one in the target collection, a duplicate key error is returned. The error message should be updated to indicate that this is expected due to the documents matching in this case. |
| Comments |
| Comment by Githook User [ 22/Nov/21 ] | |||||||||||||
|
Author: {'name': 'Hana Pearlman', 'email': 'hana.pearlman@mongodb.com', 'username': 'HanaPearlman'}Message: | |||||||||||||
| Comment by Charlie Swanson [ 26/Aug/21 ] | |||||||||||||
|
Throwing this on the backlog - I think we could reasonably detect when the duplicate key is on the "on" field and give a more useful error message indicating that we are failing because you asked us to fail in this scenario, not because something else went wrong. | |||||||||||||
| Comment by Alice Thum [ 19/Aug/21 ] | |||||||||||||
|
I was thinking it would be nice to see something similar to "$merge could not find a matching document in the target collection for at least one document in the source collection" - which is what gets returned when { "whenNotMatched": fail }. It would be easier to glean that it was not user error (e.g. duplicate ids or keys) that caused the operation to fail, but rather that we found a matching document - this would especially be helpful in a larger collection with more complicated schema/$merge parameters. That being said, it seems like this wouldn't work super well for the server's internal $merge implementation so I'm fine closing this ticket if there's no clear path to expose something like that! | |||||||||||||
| Comment by Charlie Swanson [ 19/Aug/21 ] | |||||||||||||
|
Got it, thanks! Do you think adding some context like this would be good enough?
Or were you hoping to see specifically some text around "the target value was found and you specified to error"? I think your example with _id shows how it could be tricky - we can't necessarily use DuplicateKey as evidence that we found a match. It may be possible but it's not obvious to me. | |||||||||||||
| Comment by Alice Thum [ 19/Aug/21 ] | |||||||||||||
|
I changed the "_id" of the document in the target collection to 4, so all "_id"s would be unique. The same query then fails with
| |||||||||||||
| Comment by Charlie Swanson [ 19/Aug/21 ] | |||||||||||||
|
alice.thum it looks like the duplicate key is coming from the id, not the "a" field which is the one given as the "on" option. If there is a duplicate key only on "a", is the error message any better/different? |