【问题标题】:Mongoose sort and get only one per field猫鼬排序,每个字段只得到一个
【发布时间】:2018-06-15 15:33:05
【问题描述】:

我需要使用 mongoose 获取项目列表,但是,我只需要选择任何类型的一个项目(我有 5 个可能的值),并且始终是该类型的最后插入的项目。

例如,到下面的列表中,没有排序(我知道,这里是排序的)。

[
  {
    "_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"
  }
]

预期的结果是这样的:

[
  {
    "_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"
  }
]

基本上每种类型只有一个,按id排序

我该怎么做?

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    您可以在mongodb中使用$group来区分documentType

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

    输出将是

    [
      {
        "_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"
      }
    ]
    

    【讨论】:

    • 我把我的问题描述错了,我需要的是这个,但是,我想要一个按 createdAt 的 desc 排序,而不是按 id 排序
    • 我已经解决了 { $sort: { createdAt: -1, }, },
    猜你喜欢
    • 2020-03-13
    • 2017-02-10
    • 2016-05-03
    • 1970-01-01
    • 2023-03-08
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    相关资源
    最近更新 更多