I have a simple document that stores arrays of objects for a user, and I am looking to get a sum of these arrays and a total of all; the challenge is that some documents are missing fields that other documents have, and this causes my aggregate query to fail.
Document looks like this.
{
name: '',
type: '',
cars: [],
boats: [],
planes: []
}
Some people do not have boats or planes...and those documents might look like
{
name: '',
type: '',
cars: []
}
So when I run my aggregate
[
{
'$match': {
'type': 'big_spender'
}
}, {
'$project': {
'name': '$name',
'cars': {
'$size': '$cars'
},
'boats': {
'$size': '$boats'
},
'planes': {
'$size': '$planes'
}
}
}, {
'$addFields': {
'total_vehicles': {
'$add': [
'$cars', '$boats', '$planes'
]
}
}
}
]
I get the error: "The argument to $size must be an array, but was of type: missing"
I am pretty sure I can use $exists to avoid this problem, and return a 0, but I have no idea what that syntax might look like.
I need to return a 0 for arrays that don't exist so when i add them for totals, I get a correct total and no errors.
Any help appreciated.