[SERVER-27136] preserveNullAndEmptyArrays not working correctly Created: 21/Nov/16  Updated: 02/Dec/16  Resolved: 02/Dec/16

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: guru rajan Assignee: Charlie Swanson
Resolution: Incomplete Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 Description   

Input :

db.inventory.insert([
{ "_id" : 1, "item" : "ABC", "sizes": [ "S", "M", "L"] },
{ "_id" : 2, "item" : "EFG", "sizes" : [ ] },
{ "_id" : 3, "item" : "IJK", "sizes": "M" },
{ "_id" : 4, "item" : "LMN" },
{ "_id" : 5, "item" : "XYZ", "sizes" : null }
]);

Query to execute:

db.inventory.aggregate( [
   { $unwind: { path: "$sizes", preserveNullAndEmptyArrays: true } }
] )

Output :

{ "_id" : 1, "item" : "ABC", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC", "sizes" : "L" }
{ "_id" : 2, "item" : "EFG" }
{ "_id" : 3, "item" : "IJK", "sizes" : "M" }
{ "_id" : 4, "item" : "LMN" }
{ "_id" : 5, "item" : "XYZ", "sizes" : null }

Doubt : Here for the _id : 2 there is an empty array [] for sizez, Now it is eradicated, Actually it should be there like how null values are retained, Then only we will know the value of sizes right, Why it is removing that field



 Comments   
Comment by Charlie Swanson [ 02/Dec/16 ]

Hi guruskec,

I haven't heard back in a while with any justification for making this backwards breaking change, so I'm going to close the ticket. If you can provide more information as requested in my previous comments, I will re-open this ticket and continue to investigate. Thanks.

cc other user names guruskec@gmail.com and rajan.job999@gmail.com.

Comment by Charlie Swanson [ 28/Nov/16 ]

Hi guruskec@gmail.com,

I haven't heard back in a little while. Can you please respond to my comment above? I'd like to hear some argument for why we should change the behavior of this option.

Comment by Charlie Swanson [ 21/Nov/16 ]

Hi guruskec,

This is the intended behavior of this option. If I remember correctly, the rationale for this decision was that a scalar value like "L" or null might be used in place of a singleton array, and should be treated as equivalent to ["L"] or [null]. An empty array on the other hand was likely to be used as a placeholder for future results to be put into, thus there are no values yet, so there should be no value in the unwound result.

Another good thing about this behavior is that it will combine nicely with a $lookup stage to give a left-outer join. If a document passes through the $lookup stage without any matches then the 'as' path is unwound, it seems more intuitive to me that the 'as' path would be missing (or perhaps null?) than for it to have an empty array in it.

I acknowledge that the name is confusing, as it does not actually preserve the value of the empty array, but it was meant to convey that it preserves the presence of the document containing the empty array.

Can you provide some reason why it would be more useful to have the empty array output?

Generated at Thu Feb 08 04:14:16 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.