fakemongo / fongo

faked out in-memory mongo for java
Apache License 2.0
523 stars 155 forks source link

$size operator requires an array of 1 operand #358

Open ledomi opened 5 years ago

ledomi commented 5 years ago

Hi All,

I think that validation for aggregation with $size is invalid. Step by step:

Fongo version: 2.1.1

My collection:

[{
        "_id": 5b894357a0c84d5a5d221f25,
        "conferenceName": "myFirstConference",
        "startDate": 1535722327,
        "endDate": 1535722420,
        "participants": [{
                "name": "user1",
                "origin": "internal",
                "ip": "192.168.0.2"
            }, {
                "name": "user2",
                "origin": "external",
                "ip": "172.20.0.3"
            },{
                "name": "user3",
                "origin": "internal",
                "ip": "192.168.0.3"
            },
        ]
    }
]

My mongo query that works is :

db.collection.aggregate([{
        "$project": {
            "conferenceName": 1,
            "startDate": 1,
            "endDate": 1,
            "internalUsersCount": {
                "$size": {
                    "$filter": {
                        "input": "$participants",
                        "as": "part",
                        "cond": {
                            "$eq": ["$$part.origin", "internal"]
                        }
                    }
                }
            }
        }
    }])

In that case as an input we have a LinkedHasMap not List but parsing was implementet as below

class ProjectedSize 
.....
 private String parseOperand(Object value) {
      if (!(value instanceof String) && (!(value instanceof List) || ((List) value).size() != 1)) {
        errorResult(coll, 16020, "the " + keyword + " operator requires an array of 1 operand");
      }
}

But it doesn't work in fongo, Anybody can help ?