[SERVER-20014] Question about aggregation behavior Created: 18/Aug/15 Updated: 21/Sep/15 Resolved: 21/Sep/15 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Aggregation Framework |
| Affects Version/s: | 3.0.5 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Minor - P4 |
| Reporter: | Ben Rotz | Assignee: | Asya Kamsky |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Operating System: | ALL |
| Participants: |
| Description |
|
The following behavior seems like a bug to me:
Why does "thing" get set to an empty array? It should be null? I ran into this problem while doing a $push like the following during a $group phase
and getting an empty array for warehouse often times instead of a string when sku.inventory was an empty array. |
| Comments |
| Comment by Ramon Fernandez Marina [ 21/Sep/15 ] | ||||||||||
|
This is (currently) the expected behavior, so I'm closing this ticket as "Works as Designed". Regards, | ||||||||||
| Comment by Ben Rotz [ 19/Aug/15 ] | ||||||||||
|
Since $myarray.made_up_field is null (does not exist), I would expect "thing" to be null as well. The problem I was experiencing was really due to the fact that the $unwind operator returns no rows for an empty array field (as explained here: http://stackoverflow.com/questions/22114748/unwind-empty-array-in-mongodb) , and the only way around that behavior is to project like so:
Thereby making sku.inventory have an "empty value" so when I unwound it, it didn't disappear. But, I was a dummy and was using [ [ ] ] instead of [ { } ] as my value, as I should have done. So I was able to make it work. It just threw me for a loop that when doing $first or $push on an an undefined field (even if it is a child of an array field that does not exist), it should be null, right? Overall, weird use case and not a big deal. As mentioned, if you set myarray/myobj to an empty object instead of an empty array, $first/etc. results in null as expected.
| ||||||||||
| Comment by Asya Kamsky [ 18/Aug/15 ] | ||||||||||
|
nefiga it seems to me like this is doing the correct thing - can you explain why you don't expect an array when you're pushing an array (or giving $array to $first)? |