3
const eventSchema = new Schema({
  id: Schema.Types.ObjectId,
  serviceName: String,
  event: {
    type: { type: String, required: true },
    subType: { type: String, required: true },
  },
  properties: Object,
  created_at: { type: Date, default: Date.now() },
})

Above is the schema for Event Model. how can i get the latest N no of distinct record ( distinct by type & subType) with created_at date?

shrestha rohit
  • 2,920
  • 2
  • 13
  • 20
  • can you share some sample data and expected output? – varman Sep 15 '20 at 04:39
  • @varman, https://imgur.com/mgBA7p5.png please look the above url for sample data. As you see in the screenshot above there are two documents with same `type` and `subType` (type: VIDEO, subType: PLAYED) . we must only get one latest event type & subType. Output may be similar to ``` [ { event: { type: 'EBOOK', subType: 'ACCESSED' }, created_at: date }, { event: { type: 'VIDEO', subType: 'PLAYED' }, created_at: date } ] ``` – shrestha rohit Sep 15 '20 at 04:51
  • can you confirm again in your sample data, there are no any similar type and subType documents. – turivishal Sep 16 '20 at 09:04
  • @turivishal, added sample data in the question. – shrestha rohit Sep 16 '20 at 09:32

0 Answers0