如何在 MongoDB 中对内部数组进行排序?


您可以借助 MongoDB 中的聚合框架来实现此目的。为了理解它,让我们创建一个包含文档的集合。用于创建包含文档的集合的查询如下

> db.sortInnerArrayDemo.insertOne(
...
...    {
...       "EmployeeDetails":
...       {
...          "EmployeeAddress":
...          {
...             "EmployeeCountry":
...             [
...                {
...                   "EmployeeZipCode":1003,
...                   "EmployeeStreetName":"7885 Trusel Street"
...                },
...                {
...                   "EmployeeZipCode":1001,
...                   "EmployeeStreetName":"7390 Gonzales Drive"
...                },
...                {
...                   "EmployeeZipCode":1002,
...                   "EmployeeStreetName":"444 N.Myres Rd."
...                }
...             ]
...          }
...       }
...    }
...
... );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c6f07d3da34711ecf87a5b8")
}

使用 find() 方法显示集合中的所有文档。查询如下

> db.sortInnerArrayDemo.find().pretty();

以下是输出

{
   "_id" : ObjectId("5c6f07d3da34711ecf87a5b8"),
   "EmployeeDetails" : {
      "EmployeeAddress" : {
         "EmployeeCountry" : [
         {
            "EmployeeZipCode" : 1003,
            "EmployeeStreetName" : "7885 Trusel Street"
         },
         {
            "EmployeeZipCode" : 1001,
            "EmployeeStreetName" : "7390 Gonzales Drive"
         },
         {
            "EmployeeZipCode" : 1002,
            "EmployeeStreetName" : "444 N.Myres Rd."
         }
      ]}
   }
}

以下是用于对内部数组进行排序的查询。

情况 1:按升序进行排序。

查询如下

> db.sortInnerArrayDemo.aggregate(
... {$unwind: '$EmployeeDetails.EmployeeAddress.EmployeeCountry'},
... {$sort: {'EmployeeDetails.EmployeeAddress.EmployeeCountry.EmployeeZipCode': 1}},
... {$group: {_id: '$_id', 'EmpCountry': {$push:
'$EmployeeDetails.EmployeeAddress.EmployeeCountry'}}},
... {$project: {'EmployeeDetails.EmployeeAddress.EmployeeCountry':
'$EmpCountry'}}).pretty();

以下是输出,显示内部数组基于 EmployeeZipCode 按升序排序

{
   "_id" : ObjectId("5c6f07d3da34711ecf87a5b8"),
   "EmployeeDetails" : {
      "EmployeeAddress" : {
         "EmployeeCountry" : [
            {
               "EmployeeZipCode" : 1001,
               "EmployeeStreetName" : "7390 Gonzales Drive"
            },
            {
               "EmployeeZipCode" : 1002,
               "EmployeeStreetName" : "444 N.Myres Rd."
            },
            {
               "EmployeeZipCode" : 1003,
               "EmployeeStreetName" : "7885 Trusel Street"
            }
         ]
      }
   }
}

情况 2:按降序进行排序

查询如下

> db.sortInnerArrayDemo.aggregate(
... {$unwind: '$EmployeeDetails.EmployeeAddress.EmployeeCountry'},
... {$sort: {'EmployeeDetails.EmployeeAddress.EmployeeCountry.EmployeeZipCode':-1}},
... {$group: {_id: '$_id', 'EmpCountry': {$push:
'$EmployeeDetails.EmployeeAddress.EmployeeCountry'}}},
... {$project: {'EmployeeDetails.EmployeeAddress.EmployeeCountry':
'$EmpCountry'}}).pretty();

以下是输出,显示内部数组基于 EmployeeZipCode 按降序排序

{
   "_id" : ObjectId("5c6f07d3da34711ecf87a5b8"),
   "EmployeeDetails" : {
      "EmployeeAddress" : {
         "EmployeeCountry" : [
            {
               "EmployeeZipCode" : 1003,
               "EmployeeStreetName" : "7885 Trusel Street"
            },
            {
               "EmployeeZipCode" : 1002,
               "EmployeeStreetName" : "444 N.Myres Rd."
            },
            {
               "EmployeeZipCode" : 1001,
               "EmployeeStreetName" : "7390 Gonzales Drive"
            }
         ]
      }
   }
}

更新日期:2019-07-30

3 千以上的观看次数

开启您的 职业生涯

完成课程获得认证

开始
广告