MongoDB-$ size的参数必须是一个数组,但类型为:EOO/缺少 [英] MongoDB - The argument to $size must be an Array, but was of type: EOO / missing
问题描述
尝试使用 icCube 创建MongoDB数据源.想法是将数组的大小作为新字段返回.像这样的东西:
Trying to create a MongoDB data source with icCube. The idea is to return the size of an array as a new field. Something like :
$project:
{
"people": 1,
"Count myFieldArray" : {$size : "$myFieldArray" }
}
但是对于某些记录,我得到了以下错误:
But I'm getting for some records the following error :
The argument to $size must be an Array, but was of type: EOO
如果字段为空或不是数组(摆脱错误),有没有办法将size设为0?
Is there a way that size is 0 if the field is empty or not an array (getting rid of the error) ?
推荐答案
您可以使用 $ifNull
运算符.似乎该字段不是数组,或者不是由给定的错误提供的:
You can use the $ifNull
operator here. It seems the field is either not an array or not present by the given error:
{ "$project": {
"people": 1,
"Count": {
"$size": { "$ifNull": [ "$myFieldArray", [] ] }
}
}}
此外,您可能还需要检查 $type
如果您的 $match
中的a>但不是数组.
Also you might want to check for the $type
in your $match
in case these do exist but are not an array.
这篇关于MongoDB-$ size的参数必须是一个数组,但类型为:EOO/缺少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!