Litote / kmongo

[deprecated] KMongo - a Kotlin toolkit for Mongo
https://litote.org/kmongo/
Apache License 2.0
781 stars 75 forks source link

Max should take multiple fields rather than a field and a constant #381

Closed jdpearce closed 1 year ago

jdpearce commented 2 years ago

I'm having to use a query string in an aggregation because the max operator here only takes a single field and I need it to take two fields.

[
  { ${MongoOperator.match}: { createdAt: { ${MongoOperator.gte}: new Date("$startDate") } } },
  { ${MongoOperator.group}: { _id: "$period", timeSaved: { ${MongoOperator.sum}: { ${MongoOperator.max}: [{ ${MongoOperator.trunc}: { ${MongoOperator.divide}: ["$ timeSaved", 1000]} }, "$ seconds"]} } } },
]

I can replicate the $match clause in KMongo, but I cannot replicate the $group because max seems to only take a field and a constant? Is there a way to replicate this query with KMongo?

zigzago commented 1 year ago

Hello,

You can use the magic "from". It should work:

       MongoOperator.max.from(
           listOf(
               MongoOperator.trunc.from(MongoOperator.divide(listOf(A::timeSaved.projection, 1000))),
               A::seconds.projection    
           )
       )

HTH