[SERVER-67379] Accessing array element with invalid methods Created: 18/Jun/22 Updated: 27/Oct/23 Resolved: 01/Sep/22 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | None |
| Affects Version/s: | 5.0.9, 4.4.15 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Pooyan Khanjankhani | Assignee: | Yuan Fang |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Attachments: |
|
||||||||
| Issue Links: |
|
||||||||
| Operating System: | ALL | ||||||||
| Participants: | |||||||||
| Description |
|
As we know proper way to access a array element in aggregation runtime is via using `$arrayElemAt` operator. but in situations which I have tried with invalid methods like `$$arr.0` or `$$arr.[0]` I expect to get some kind of error or maybe similar behaviour as using `$$arr[0]` which does nothing but except with two latter methods of accessing array element I would get an empty array as result which I consider to be some sort of bug or invalidAccessMethodWhichDoesSomethingWierd:something. Or... there is some unknown defined behaviour which I'm not aware of! Currently I checked this with mongod version 4.4(for sure) and 5.0.9(I think so...) and result was the same. As you see in the picture on right terminal I set with 3 methods which 2 of them are invalid but In those 2 invalid situations I get different responses. neither of them throws any error... |
| Comments |
| Comment by Pooyan Khanjankhani [ 02/Sep/22 ] | |||||
|
Thanks for your response and sorry for thinking it was as issue, next time I'll ask in community forums first😘🙏 | |||||
| Comment by Yuan Fang [ 31/Aug/22 ] | |||||
|
Hi pooyankhan@gmail.com , Thank you for reporting this issue. If I understand correctly, you expected to get the same output for "$arr.0" and "$arr['0']". However, I believe the different outputs are expected because:
Here is an example of aggregating values with field '0' for objects in arr:
As you mentioned, both of these expressions will not work as intended for getting a specific element in the array in aggregation. I would suggest using the operator $arrayElemAt to avoid getting unexpected results. For issues you may encounter in the future we'd like to encourage you to start by asking our community for help by posting on the MongoDB Developer Community Forums. If the discussion there leads you to suspect a bug in the MongoDB server, then we'd want to investigate it as a possible bug here in the SERVER project. Regards, Yuan |