[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: PNG File mongo-issue.png    
Issue Links:
Related
related to SERVER-27442 Positional path component in match la... Backlog
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:

  • "$arr.0" - Attempts to aggregate objects containing field "0" in array arr. This produces an empty array because there are no objects in arr that have a "0" field.

           Here is an example of aggregating values with field '0' for objects in arr:

{ arr: [{ '0': 1 }, { '0': 5 }, { '0': 10 }] }
 
db.test_col.aggregate([{ $set: { newField: "$arr.0" } }]);
 
> { "newField" : [ 1, 5, 10 ] }

 

  • "$arr['0']" - Attempts to access a field named "$arr['0']". This doesn't produce a result because there is no field that matches the string of "$arr['0']".

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

Generated at Thu Feb 08 06:07:59 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.