MongoDB - $size 的参数必须是数组,但类型为:EOO/缺失 [英] MongoDB - The argument to $size must be an Array, but was of type: EOO / missing

查看:15
本文介绍了MongoDB - $size 的参数必须是数组,但类型为:EOO/缺失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用 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

如果字段为空或不是数组,有没有办法将大小设为 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 如果这些确实存在但不是数组.

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆