77

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
  • 148,042
  • 36
  • 346
  • 317
ic3
  • 7,917
  • 14
  • 67
  • 115

3 Answers3

154

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,077
  • 22
  • 29
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • 2
    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
9

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
  • 100,957
  • 23
  • 236
  • 235
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
  • 150
  • 2
  • 6