Simão Lemos
Simão Lemos

Reputation: 1239

Mongoose sort and get only one per field

I need to get a list of item using the mongoose, however, i need select only one item of any type (i've 5 possible values), and always the last inserted item of this type.

for example, to the list below, not ordered (i know, here it's ordered).

[
  {
    "_id": "5b226abab64b2309de01bfd5",
    "documentType": "type3",
    "createdAt": "2018-06-14T13:16:42.321Z"
  },
  {
    "_id": "5b226a5da93b0e09b8aee447",
    "documentType": "type3",
    "createdAt": "2018-06-14T13:15:09.458Z"
  },
  {
    "_id": "5b226a21f454d6097ffa461c",
    "documentType": "type2",
    "createdAt": "2018-06-14T13:14:09.159Z"
  },
  {
    "_id": "5b2267fb445d7709590e1695",
    "documentType": "type1",
    "createdAt": "2018-06-14T13:04:59.742Z"
  },
  {
    "_id": "5b2267de76708b094696b0fe",
    "documentType": "type3",
    "createdAt": "2018-06-14T13:04:30.349Z"
  },
  {
    "_id": "5b2267ce2f3724092b1a14a3",
    "documentType": "type2",
    "createdAt": "2018-06-14T13:04:14.410Z"
  },
  {
    "_id": "5b2267a92f3724092b1a14a2",
    "documentType": "type4",
    "createdAt": "2018-06-14T13:03:37.079Z"
  },
  {
    "_id": "5b22647b63017e08a69a3043",
    "documentType": "type5",
    "createdAt": "2018-06-14T12:50:03.999Z"
  },
  {
    "_id": "5b2264471778a20880d4ba64",
    "documentType": "type5",
    "createdAt": "2018-06-14T12:49:11.773Z"
  }
]

the expected result is something like this:

[
  {
    "_id": "5b226abab64b2309de01bfd5",
    "documentType": "type3",
    "createdAt": "2018-06-14T13:16:42.321Z"
  },
  {
    "_id": "5b226a21f454d6097ffa461c",
    "documentType": "type2",
    "createdAt": "2018-06-14T13:14:09.159Z"
  },
  {
    "_id": "5b2267fb445d7709590e1695",
    "documentType": "type1",
    "createdAt": "2018-06-14T13:04:59.742Z"
  },
  {
    "_id": "5b2267a92f3724092b1a14a2",
    "documentType": "type4",
    "createdAt": "2018-06-14T13:03:37.079Z"
  },
  {
    "_id": "5b22647b63017e08a69a3043",
    "documentType": "type5",
    "createdAt": "2018-06-14T12:50:03.999Z"
  }
]

Basicly only one per type, sort desc by id

How can i do this?

Upvotes: 1

Views: 56

Answers (1)

Ashh
Ashh

Reputation: 46441

You can use $group by in mongodb to distinct the documentType

db.collection.aggregate([
  { "$group": {
    "_id": "$documentType",
    "createdAt": { "$first": "$createdAt" },
    "id": { "$first": "$_id" }
  }},
  { "$project": {
    "_id": "$id",
    "createdAt": 1,
    "documentType": "$_id"
  }}
])

The output will be

[
  {
    "_id": "5b226abab64b2309de01bfd5",
    "createdAt": "2018-06-14T13:16:42.321Z",
    "documentType": "type3"
  },
  {
    "_id": "5b2267fb445d7709590e1695",
    "createdAt": "2018-06-14T13:04:59.742Z",
    "documentType": "type1"
  },
  {
    "_id": "5b22647b63017e08a69a3043",
    "createdAt": "2018-06-14T12:50:03.999Z",
    "documentType": "type5"
  },
  {
    "_id": "5b2267a92f3724092b1a14a2",
    "createdAt": "2018-06-14T13:03:37.079Z",
    "documentType": "type4"
  },
  {
    "_id": "5b226a21f454d6097ffa461c",
    "createdAt": "2018-06-14T13:14:09.159Z",
    "documentType": "type2"
  }
]

Upvotes: 1

Related Questions