用于组的mongo sort子文档

时间:2017-07-31 18:37:39

标签: mongodb sorting mongoose aggregate

我有一个成员集合:

{
  "presets": [
    "react",
    "latest"
  ],
  "plugins": [
    "transform-object-rest-spread",
    "transform-runtime"
  ]
}

队长架构属性引用同一集合中的另一个文档。

我还有一个订单集合:

{
    "_id": ObjectId("597cace8e68903af813c4fe8"),
    "lastName": "Smith",
    "firstName": "Lloyd",
    "captain": ObjectId("597cace8e68903af813c4fe8")
} 
{
    "_id": ObjectId("597cace8e68903af813c4fec"),
    "lastName": "Jones",
    "firstName": "Chris",
    "captain": ObjectId("597cace8e68903af813c4fe8")
}

memberId 是引用成员集合的外键。

我正在准备一份如下所示的报告,并且由船长组成GROUPED,按升序排序船长名称(lastName,firstName)。在队长组中,我希望成员们以同样的方式排序:

{
    "_id" : ObjectId("597cf7e165b6c3f5094080c9"),
    "orderTotal" : 99.86,
    "orderDate" : ISODate("2017-07-29T21:02:25.045Z"),
    "orderProducts" : [
            {
                    "title" : "Beads",
                    "totalItemPrice" : 39.96
            },
            {
                    "title" : "Doubloons",
                    "totalItemPrice" : 59.9
            }
    ],
    "memberId" : ObjectId("597cace8e68903af813c4fec")
}

我有一个主要做我想要的查询,但不对成员名称进行排序

MEMBER ORDERS

Captain: Lloyd Smith

Members:
  1. Chris Jones
    * Beads: $39.96
    * Doubloons: $59.90
    * Total: $99.86
  2. Lloyd Smith
    * No Orders
  etc..

Captain: George Taylor

Members:
  1. John Brown
    * Beads: $1.96
    * Doubloons: $9.00
    * Total: $10.96
  2. Chip Castle
    * Beads: $1.00
    * Doubloons: $9.00
    * Total: $10.00
  etc...

除了我的排序问题之外,我愿意接受任何有关提高管道可读性和性能的建议。

1 个答案:

答案 0 :(得分:0)

我终于明白了!我需要 $ unwind 成员 $ sort ,以及其他一些步骤,以便将数据恢复到所需的结构中。

Member.aggregate([
  {
      $lookup : {
        from : "orders",
        localField : "_id",
        foreignField : "kreweOfTucksId",
        as : "tmpOrders"
      }
  },
  {
      $lookup : {
        from : "members",
        localField : "captain",
        foreignField : "_id",
        as : "tmpCaptain"
      }
  },
  {
      $group: {
          _id: "$captain",
          captain: {
              $addToSet: {
                  firstName: "$tmpCaptain.firstName",
                  lastName: "$tmpCaptain.lastName"
              }
          }
          ,
          members: {
              $addToSet: {
                  _id: "$_id",
                  firstName: "$firstName",
                  lastName: "$lastName",
                  captain: "$captain",
                  orders: "$tmpOrders",

              }
          }
      }
  },
  {
      $unwind: "$members"
  },
  {
      $sort: {
          "members.lastName": 1,
          "members.firstName": 1
      }
  },
  {
      $group: {
          _id: "$captain",
          members: {
              $push: "$members"
          }
      }
  },
  {
      $sort: {
          "_id.lastName": 1,
          "_id.firstName": 1
      }
  },
  {
      $project: {
        _id: 1,
        "members.firstName": 1,
        "members.lastName": 1,
        "members.orders.orderTotal": 1,
        "members.orders.orderDate": 1,
        "members.orders.orderProducts.title": 1,
        "members.orders.orderProducts.totalItemPrice": 1
      }
  }
])

鉴于生成此报告所需的步骤,我敢打赌有很多方法可以改进查询。如果有任何专家有改进建议,我将不胜感激。谢谢!