简体   繁体   English

MongoDB 按字母顺序对 output 进行排序(分组后)

[英]MongoDB sorting the output alphabetically (after grouping)

I have the following data:我有以下数据:

[
  {
    "names": [
      "name1,name2,name3"
    ]
  },
  {
    "names": [
      "name1 , name2 , name3 , name4 , name5 , name6"
    ]
  }
]

Which I reduce to a set of unique values by removing whitespace and splitting by commas, using the following query:我使用以下查询通过删除空格和逗号分隔将其简化为一组唯一值:

db.collection.aggregate([
  {
    $project: {
      names: {
        $reduce: {
          input: "$names",
          initialValue: [],
          in: {
            $setUnion: [
              {
                $split: [
                  "$$this",
                  ","
                ],
                
              },
              "$$value"
            ]
          }
        }
      }
    }
  },
  {
    $unwind: "$names"
  },
  {
    $group: {
      _id: "$a",
      names: {
        $addToSet: {
          $trim: {
            input: "$names",
            chars: " "
          }
        }
      }
    }
  }
])

Everything works as you can see here: https://mongoplayground.net/p/nlK5Gmf-gkw The list of unique values produces, however, I need to sort the results alphabetically and the sorting mechanism added like this:一切正常,如您在此处看到的那样:https: https://mongoplayground.net/p/nlK5Gmf-gkw生成唯一值列表,但是,我需要按字母顺序对结果进行排序,并像这样添加排序机制:

{
    $sort: {
      "a": -1
    }
}

, doesn't seem to work. ,好像不行。

The result I get is unsorted:我得到的结果是未排序的:

[
  {
    "_id": null,
    "names": [
      "name1",
      "name5",
      "name4",
      "name6",
      "name3",
      "name2"
    ]
  }
]

Any suggestions?有什么建议么?

https://mongoplayground.net/p/nlK5Gmf-gkw https://mongoplayground.net/p/nlK5Gmf-gkw

You just need change the order of stage and operations,你只需要改变阶段和操作的顺序,

  • $group by null and use the same process that you used $reduce to split string to array and $map to remove white space from names using $trim $group by null 并使用与$reduce相同的过程将字符串拆分为数组,使用$map使用$trim从名称中删除空格
  • $project to update get unique array from all names using $reduce and $setUnion要更新的$project使用$reduce$setUnion从所有名称中获取唯一数组
  • $unwind deconstruct names array $unwind解构names数组
  • $sort by names descending order $sort按名称降序排序
  • $group by null and make array of names $group by null 并创建names数组
db.collection.aggregate([
  {
    $group: {
      _id: null,
      names: {
        $push: {
          $map: {
            input: {
              $reduce: {
                input: "$names",
                initialValue: [],
                in: { $split: ["$$this", ","] }
              }
            },
            in: { $trim: { input: "$$this" } }
          }
        }
      }
    }
  },
  {
    $project: {
      names: {
        $reduce: {
          input: "$names",
          initialValue: [],
          in: { $setUnion: ["$$this", "$$value"] }
        }
      }
    }
  },
  { $unwind: "$names" },
  { $sort: { names: -1 } },
  {
    $group: {
      _id: null,
      names: { $push: "$names" }
    }
  }
])

Playground操场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM