尝试使用 icCube 创建 MongoDB 数据源.这个想法是将数组的大小作为新字段返回。类似的东西:
$project:
{
"people": 1,
"Count myFieldArray" : {$size : "$myFieldArray" }
}
但我收到以下错误记录:
The argument to $size must be an Array, but was of type: EOO
如果字段为空或不是数组,有没有办法将大小设为 0(消除错误)?
最佳答案
您可以使用 $ifNull
运营商在这里。似乎该字段不是数组或给定错误不存在:
{ "$project": {
"people": 1,
"Count": {
"$size": { "$ifNull": [ "$myFieldArray", [] ] }
}
}}
您可能还想检查 $type
在您的$match
如果这些确实存在但不是数组。
https://stackoverflow.com/questions/24201120/