R
R
Roman Andreevich2020-07-06 08:15:01
Mongoose
Roman Andreevich, 2020-07-06 08:15:01

How to make a request in Mongoose?

You need to take the last 50 records from the collection and group them by field.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-07-06
@hzzzzl

there is little information about the structure of the collection, but in general - the so-called aggregation pipeline
did this with its local database of Deals offers, grouped by city

Deal.aggregate([
  {
    '$sort': {
      '_id': -1    // тупо по айди, если есть поле в духе created_at, то использовать его
    }
  }, {
    '$limit': 50
  }, {
    '$group': {
      '_id': '$city',    // сюда поле
      'data': {
        '$push': '$$ROOT'   // это чтобы добавился массив с записями в результат
      }
    }
  }
])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question