I'm trying to write an aggregate query using $unwind no matter the element is an array or not. I know $unwind does not work on no array elements, but I wonder if there is a way to make it work, like converting this element into an array.
I have a collection like this:
{
{"x" : 1, "y" : {"c" : 2, "i" : 3}},
{"x" : 1, "y" : [{"c" : 4, "i" : 5}, {"c" : 6, "i" : 7}]}
}
Before I $unwind I think I need something like this:
{
{"x" : 1, "y" : [{"c" : 2, "i" : 3}]},
{"x" : 1, "y" : [{"c" : 4, "i" : 5}, {"c" : 6, "i" : 7}]}
}
So far in $project stage I can check if element is array or not, but I don't know how to use this information to make an array or not. I know I can use $push to make an array, but how to leave untouched arrays elements and just $push no array elements?
I tried this:
{$group : {"_id" : "$x", "myArray" : {$push : {$cond : {if : "$isArray", then : "$y", else : ["$y"]}}}}}
With the code above I tried to have all elements in the same level but did not work since ["$y"] returns exactly that (["$y"]), no evaluation, just an array with a string in it.
I don't want to $unwind an empty array, I want to convert a non array element into an array element so I can $unwind.
any help will be appreciated.