[SERVER-43931] $elemMatch object can treat '0' as an array index Created: 10/Oct/19 Updated: 06/Dec/22 |
|
| Status: | Backlog |
| Project: | Core Server |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Ali Rahbari | Assignee: | Backlog - Query Optimization |
| Resolution: | Unresolved | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Assigned Teams: |
Query Optimization
|
| Sprint: | Query 2019-11-04 |
| Participants: |
| Description |
|
Consider following document:
I want to remove all items which their 0 index is 2.
this does nothing.
this oddly removes first item of items.
this works as expected.
|
| Comments |
| Comment by David Storch [ 22/Oct/19 ] | ||||||||||
|
Thanks nicholas.zolnierz. I agree that it makes sense to consider as part of a larger project around positional path component. | ||||||||||
| Comment by Nicholas Zolnierz [ 21/Oct/19 ] | ||||||||||
|
This appears to be another case of field name/array index ambiguity and implicit array traversal. Unfortunately, as others have stated, there's currently no way to use $elemMatch to match against a particular scalar value within an array. Since the $pull applies the $elemMatch query to each array within 'items', we can simplify the example to the following:
In the first query, the '0' in the $elemMatch predicate matches the first doc because the sub-array [2] has a value of 2 at the index 0. This is consistent with the "odd" example given in the description. Notice, however, that the '0' is also treated as a field name to match the second document. As such I don't think the title of this ticket is quite accurate. david.storch I think this ticket is superseded by either SERVER-4753 or SERVER-28779, so perhaps we can hold it in the "Positional path syntax" project for now and consider consolidating later? | ||||||||||
| Comment by Carl Champain (Inactive) [ 10/Oct/19 ] | ||||||||||
|
Thanks for the report.
$set replaces the value of items with the new filtered array. $filter returns the arrays where the element at index 0 is not equal to 2. The '0':2 in $elemMatch is a bug though; the number 0 shouldn't be treated as an array index, it should be treated only as a field name. I'll pass this ticket along to the Query team for further consideration. Please watch it for updates. Kind regards, |