[SERVER-65423] Cannot create field in element. Created: 11/Apr/22 Updated: 27/Oct/23 Resolved: 14/Apr/22 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Roman Kazanovskyi | Assignee: | Chris Kelly |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Operating System: | ALL |
| Participants: |
| Description |
|
Test scenario:
But documentation for $set says:
|
| Comments |
| Comment by Chris Kelly [ 14/Apr/22 ] | ||||||||||
|
Hello roman.kazanovskyi@aifora.com, I was able to reproduce this behavior on MongoDB 5.0.2. While the documentation is correct in that MongoDB will automatically create the subdocument for you if it doesn't exist using $set with dotted notation, it will only do so for non-existent fields. Even though your field "attr" has a value of null, the field itself exists at this point. If you want to use dotted notation:
If you want to write the whole document you wish to set instead:
I can see how the statement you shared from the documentation may be strange to parse however; it appears to imply that $set will add new child fields on dotted paths since the child doesn't exist yet ("attr.key" in your example), even though the parent does exist ("attr" in this case). For now, feel free to try any of the options I listed above, or refer to our community for further help by posting on the MongoDB Developer Community Forums. Thank you for the report! Regards, |