【问题标题】:Sorting nested array in mongodb在mongodb中对嵌套数组进行排序
【发布时间】:2020-02-06 01:30:18
【问题描述】:

我在 mongodb 集合中有以下结构,如附图所示: enter image description here

我将有一个索引号,在其中,我将有记录,现在记录将是一个或多个数组,如上所示。 我想对数学科目中每个分数的记录中的数据进行排序。 做这个的最好方式是什么? 谢谢

【问题讨论】:

    标签: python mongodb sorting nested pymongo


    【解决方案1】:

    你可以使用unwind聚合

        db.collection.aggregate([
           {
              $unwind:{
                 path: "$Records",
                 includeArrayIndex:"firstUnwind"
             }
           },
           {
              $unwind:{
                 path: "$Records",
             }
           },
          { $sort : { Match: -1} },
          {
              $group: {
                 _id: {
                    id: "$_id",
                    firstUnwind: "firstUnwind"
                 },
                 Index_Number: {$first: "$Index_Number"},
                 Records: {$push: "$Records"}
              }
          },
          {
              $group: {
                _id: "$_id.id",
                 Index_Number: {$first: "$Index_Number"},
                 Records: {$push: "$Records"}
              }
          }
        ])`
    

    【讨论】:

      猜你喜欢
      • 2020-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-26
      • 1970-01-01
      • 2014-03-10
      • 2015-08-27
      • 1970-01-01
      相关资源
      最近更新 更多