fakemongo / fongo

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

$substract with dates in $project agregation #291

Open Natheee opened 7 years ago

Natheee commented 7 years ago

I have a collection who looks like that:

{
    "_id": ObjectId("59721ee8c1970d1f84c0b192"),
    "arrTh": ISODate("2017-07-21T23:56:23Z"),
    "arrRe": ISODate("2017-07-21T23:55:56Z"),
    "depTh": ISODate("2017-07-21T23:56:45Z"),
    "depRe": ISODate("2017-07-21T23:56:52Z")
}
{
    "_id": ObjectId("59721ee8c1970d1f84c0b193"),
    "arrTh": ISODate("2017-07-22T00:02:34Z"),
    "arrRe": ISODate("2017-07-21T23:59:56Z"),
    "depTh": ISODate("2017-07-21T23:56:45Z"),
    "depRe": ISODate("2017-07-21T23:56:52Z")
}
{
    "_id": ObjectId("59721ee8c1970d1f84c0b194"),
    "arrTh": ISODate("2017-07-22T23:56:28Z"),
    "arrRe": ISODate("2017-07-22T23:55:57Z"),
    "depTh": ISODate("2017-07-22T23:56:47Z"),
    "depRe": ISODate("2017-07-22T23:56:58Z")
}

I'm trying to execute the following aggregation pipeline:

{
    "aggregate": "__collection__",
    "pipeline": [{
            "$project": {
                "id": 1,
                "arrivingGap": {
                    "$subtract": ["$depTh", "$depRe"]
                },
                "departureGap": {
                    "$subtract": ["$arrTh", "$arrRe"]
                },
                "depTh": 1,
                "depRe": 1,
                "arrTh": 1,
                "arrRe": 1
            }
        }
    ]
}

But i get an exception on the substraction code. I think that only substractions between number is working.

java.lang.ClassCastException: java.util.Date cannot be cast to java.lang.Number
    at com.github.fakemongo.impl.aggregation.Project$ProjectedToSubtract.unapply(Project.java:723)
    at com.github.fakemongo.impl.aggregation.Project.apply(Project.java:879)
    at com.github.fakemongo.impl.Aggregator.computeResult(Aggregator.java:48)
    at com.mongodb.FongoDB.doAggregateCollection(FongoDB.java:114)
    at com.mongodb.FongoDB.command(FongoDB.java:251)
    at com.mongodb.DB.command(DB.java:507)
    at com.mongodb.DB.command(DB.java:462)
    at org.springframework.data.mongodb.core.MongoTemplate$3.doInDB(MongoTemplate.java:390)
    at org.springframework.data.mongodb.core.MongoTemplate$3.doInDB(MongoTemplate.java:388)
    at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:446)
    at org.springframework.data.mongodb.core.MongoTemplate.executeCommand(MongoTemplate.java:388)
    at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1552)
    at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1487)

It's working with a real Mongod server.