'MongoDB Query to SpringData Query

for an university project I am trying to find hotel rooms which are not booked in a specific time period and city.

Therefore, I am trying to translate the following MongoDB Query into SpringData:

 db.hotel.aggregate([
{$unwind: "$rooms"},
    {"$match": {"city": "Vienna"}},
    {"$match": {"rooms.bookings.startDate": {$not: {"$gte": new Date("2022-01-20"), $lt:new Date("2022-01-29")}}}},
    {"$match": {"rooms.bookings.endDate": {$not: {"$gt": new Date("2022-01-20"), $lte:new Date("2022-01-29")}}}}
])

This is was my approach:

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startDate = LocalDateTime.parse("2022-01-21 00:00:00",formatter);
        LocalDateTime endDate = LocalDateTime.parse("2022-01-29 00:00:00",formatter);

        MatchOperation matchOperation1 = Aggregation.match(
                Criteria.where("city").is("Vienna")
                        .and("rooms.bookings.startDate").not()
                        .gte(startDate).lt(endDate)
                        .and("rooms.bookings.endDate").not()
                        .gt(startDate).lte(endDate)
        );

        Aggregation aggregation = Aggregation.newAggregation(Aggregation.unwind("rooms"),matchOperation1);

This is how SpringData translates the query:

db.hotel.aggregate([
    { "$unwind" : "$rooms"},
    { "$match" :
            {
                "city" : "Vienna",
                "rooms.bookings.startDate" :
                    {"$not" :
                            {
                                "$gte" : { "$java" : 2022-01-21T00:00 }
                            },
                                "$lt" : { "$java" : 2022-01-29T00:00 }
                    },
            "rooms.bookings.endDate" :
                { "$not" :
                        {
                    "$gt" : { "$java" : 2022-01-21T00:00 }
                        },
                    "$lte" : { "$java" : 2022-01-29T00:00
                }
                }
            }
    }
    ]
    )
db.hotel.aggregate([
    { "$unwind" : "$rooms"},
    { "$match" :
            {
                "city" : "Vienna",
                "rooms.bookings.startDate" :
                    {"$not" :
                            {
                                "$gte" : { "$java" : 2022-01-21T00:00 }
                            },
                                "$lt" : { "$java" : 2022-01-29T00:00 }
                    },
            "rooms.bookings.endDate" :
                { "$not" :
                        {
                    "$gt" : { "$java" : 2022-01-21T00:00 }
                        },
                    "$lte" : { "$java" : 2022-01-29T00:00 }
                }
            }
    }
    ]
    )

What could be the reason for this? Or is my SpringData translation completely wrong and get similiar result by coincidence?

Thanks a lot in advance! :)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source