75

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

Is there a way that size is 0 if the field is empty or not an array (getting rid of the error) ?

Neil Lunn
  • 140,271
  • 35
  • 313
  • 302
ic3
  • 7,489
  • 11
  • 62
  • 109

3 Answers3

145

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", [] ] }
    }
}}

Also you might want to check for the $type in your $match in case these do exist but are not an array.

Mike C
  • 3,027
  • 21
  • 28
Neil Lunn
  • 140,271
  • 35
  • 313
  • 302
  • 1
    Thanks a lot it works. Strange that $size of an undefined field is not 0 (actually 0 for all no arrays types). I'd expect a nonsql db to be more leniant (in case a mongo db guy reads this :-) ) – ic3 Jun 13 '14 at 09:09
  • 2
    @ic3 Not really, and that is why `$ifNull` exists. The same caveat applies to operators like `$unwind` where you need to be careful that the array is not empty or missing otherwise you will either result in removing the parent document or producing a similar error. – Neil Lunn Jun 13 '14 at 09:14
8

From MongoDB 3.2 and newer, you can use $isArray to check if your field is an array along with the $cond operator to return the field on evaluating with $isArray:

{ "$project": {
    "people": 1,
    "myFieldArrayCount": { 
        "$size": { 
            "$cond": [ 
                { "$isArray": "$myFieldArray" }, 
                "$myFieldArray", 
                []
            ]
        } 
    }
}}
chridam
  • 95,056
  • 21
  • 214
  • 219
0

Alternative solution would be to eliminate the documents with nulls using

$match: {myFieldArray: { $elemMatch: { $exists: true } }}

Also, document fields which are used as arguments to $size by '$' reference (here: "$myFieldArray") must also be the part of projections.

$project:
{ 
 "people": 1,
 "myFieldArray":1,
 "Count myFieldArray" : {$size : "$myFieldArray" }
}
Rohit Sai Janga
  • 140
  • 2
  • 6