[SERVER-13841] allow optional field specified in $addToSet operator to determine uniqueness Created: 06/May/14 Updated: 03/Apr/23 Resolved: 29/Jun/19 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Write Ops |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Zach Davis | Assignee: | Backlog - Query Team (Inactive) |
| Resolution: | Won't Do | Votes: | 9 |
| Labels: | transactions | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||
| Assigned Teams: |
Query
|
||||||||||||
| Participants: | |||||||||||||
| Description |
|
allow optional field specified in $addToSet operator to determine uniqueness Several times I've wanted to use an array of sub-documents that are uniquely identified by an ID, but the other values and fields are changing or optional. Being able to use $addToSet versus querying and updating or updating and querying would be a big win in performance. A couple examples: Array of members with optional 'name'.
I would like to be able to call update with $addToSet on the members array, without habing to worry about the presence of a 'name' determing uniqueness.
Could have an optional operator to determine uniqueness:
Another example is similar but even more of an issue for me. I would like to be able to use $inc on fields within the sub-document in the array using $addToSet in a single operation.
count for 'af39c730-928b-4d3a-acfe-6db682325a54' would be 2.
count for '828e8625-65c5-407e-a5cf-0e20d3698eaa' would be 1. |
| Comments |
| Comment by Asya Kamsky [ 29/Jun/19 ] |
|
You can see some examples here. This can be done by setting the array to its new value using various aggregation array expressions. |
| Comment by Roger Bush [ 22/Jan/17 ] |
|
A simple and useful uniqueness definition would provide the ability to specify a list of fields which are relevant in an equality/uniqueness comparison. If no fields are specified, then you'd get the default behavior (all fields are used to determine equality). This feature alone would simplify many update/insert scenarios that currently take 2 operations. The problem with using 2 operations is it introduces potential distributed race conditions. Use of this feature with a simple id almost completely eliminates any benefits of using objects versus arrays (MongoDB's feature set preferences arrays, allowing the contents to be indexed - there are also many array related features. Objects are not as supported). For example, by using the id field as the sole equality determining field, we can do an $addToSet, and in a single operation insert a unique element by id (much like what you get in a javascript object/associative array), as well as constructing the array in the initialization case. This can all be done in a single command. This would make a lot of operations more straightforward. |
| Comment by Víctor Herraiz Posada [ 10/Nov/15 ] |
|
It should be nice to have this feature and be able to change the way "$addToSet" determines uniqueness using a field restriction or a compare function. |
| Comment by Sajin [ 25/Jun/14 ] |
|
As Op mentioned several fields of objects in would be optional / timestamp field etc. |