$sort(作为聚合管道阶段)

聚合管道中的$sort阶段用于按指定的字段或字段对管道中的文档进行排序。 当需要按特定顺序显示数据时,这特别有用,例如按总销售额或按开始日期对销售事件进行排序。

语法

$sort阶段的语法如下所示:

{
  $sort: { <field1>: <sort order>, <field2>: <sort order>, ... }
}

参数

DESCRIPTION
field 要对其文档进行排序的字段。
sort order 对字段进行排序的顺序。 1 表示升序,-1 降序。

示例

示例 1:按降序按总销售额排序

若要按总销售额降序对销售类别进行排序:

db.collection.aggregate([
  {
    $unwind: "$store.sales.salesByCategory"
  },
  {
    $sort: { "store.sales.salesByCategory.totalSales": -1 }
  }
])

示例输出

[
  {
    "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
    "store": {
      "name": "Downtown Store",
      "sales": {
        "salesByCategory": [
          {
            "category": "Electronics",
            "totalSales": 15000
          },
          {
            "category": "Clothing",
            "totalSales": 12000
          },
          {
            "category": "Home Goods",
            "totalSales": 10000
          }
        ]
      }
    }
  },
  {
    "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c6",
    "store": {
      "name": "Uptown Store",
      "sales": {
        "salesByCategory": [
          {
            "category": "Electronics",
            "totalSales": 20000
          },
          {
            "category": "Clothing",
            "totalSales": 18000
          },
          {
            "category": "Home Goods",
            "totalSales": 15000
          }
        ]
      }
    }
  }
]

示例 2:按事件开始日期按升序排序

按其开始日期按其升序对促销事件进行排序:

db.collection.aggregate([
  {
    $unwind: "$store.promotionEvents"
  },
  {
    $sort: { "store.promotionEvents.promotionalDates.startDate": 1 }
  }
])

示例输出

[
  {
    "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
    "store": {
      "name": "Downtown Store",
      "promotionEvents": [
        {
          "eventName": "Summer Sale",
          "promotionalDates": {
            "startDate": ISODate("2024-06-01T00:00:00Z"),
            "endDate": ISODate("2024-06-30T23:59:59Z")
          }
        },
        {
          "eventName": "Black Friday",
          "promotionalDates": {
            "startDate": ISODate("2024-11-25T00:00:00Z"),
            "endDate": ISODate("2024-11-25T23:59:59Z")
          }
        },
        {
          "eventName": "Holiday Deals",
          "promotionalDates": {
            "startDate": ISODate("2024-12-01T00:00:00Z"),
            "endDate": ISODate("2024-12-31T23:59:59Z")
          }
        }
      ]
    }
  },
  {
    "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c6",
    "store": {
      "name": "Uptown Store",
      "promotionEvents": [
        {
          "eventName": "Back to School",
          "promotionalDates": {
            "startDate": ISODate("2024-08-01T00:00:00Z"),
            "endDate": ISODate("2024-08-31T23:59:59Z")
          }
        },
        {
          "eventName": "Winter Sale",
          "promotionalDates": {
            "startDate": ISODate("2024-12-01T00:00:00Z"),
            "endDate": ISODate("2024-12-31T23:59:59Z")
          }
        }
      ]
    }
  }
]
  • 查看从 MongoDB 迁移到 Azure Cosmos DB for MongoDB (vCore) 的选项
  • 通过创建帐户开始。