I'm looking for a Mongo equivalent of the following:
select distinct event_type_id from events
order by created_at desc
limit 10,20
The Event Mongo document is as follows:
{
_id: BSON(), event_type_id: 1, created_at: Date(), other_data: {}
}
You can use Aggregation Framework on new mongo versions. Here is the example to start from:
db.events.aggregate([
{$sort:{"created_at":-1}},
{$project:{"event_type_id":1}},
{$group:{"_id":"$event_type_id"}},
{$skip:10},
{$limit:20}
])
Here is another approach, thanks to @JoachimIsaksson
db.events.aggregate([
{$group:{"_id":"$event_type_id", "created_at":{$max:"$created_at"}}},
{$sort:{"created_at":-1}},
{$skip:10},
{$limit:20},
{$project:{"event_type_id":1}}
])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With