duyluonglc / lucid-mongo

Mongodb ODM for adonis framework
327 stars 60 forks source link

writing raw queries / aggregation #255

Closed scandar closed 3 years ago

scandar commented 4 years ago

i was trying to query a bunch of data for some analytics.

accessing the database directly with this query gets me the data i need

db.getCollection('orders').aggregate([
{
        $group : {
           _id :{ $dateToString: { format: "%Y-%m-%d", date: "$created_at"} },
           count: { $sum: 1 },
           total_price: { $sum: "$total_price" }
        }
}
])

how exactly do i translate this to the ORM?

i know there's methods like count and sum but this forces me to do the same query twice instead of getting all the results in one query

also is there an equivalent for the aggregate function?