python - 在mongodb中对嵌套数组进行排序
问题描述
我在 mongodb 集合中有以下结构,如附图所示: 在此处输入图像描述
我将有一个索引号,在其中,我将有记录,现在记录将是一个或多个数组,如上所示。我想对数学科目中每个分数的记录中的数据进行排序。做这个的最好方式是什么?谢谢
解决方案
您可以使用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"}
}
}
])`
推荐阅读
- r - 将一个 Granges 对象的多个元数据列的分数聚合到另一个上
- php - Laravel 5.8 Posting Likes with AJAX call给了我内部服务器错误500
- python-3.x - 如何制作包含 n 个项目的列表
- list - Haskell:创建列表中每个列表的最后一个元素的列表
- css - 边距不能定位按钮和文本区域
- python-3.x - Python 3.x:在一行中进行“打印”和“输入”的最简单方法是什么?
- assembly - Z80 装配程序失控
- matplotlib - python3.7如何安装matplotlib?
- hadoop - 如何修复“/hbase/master 的 NoNode”
- vb.net - Net.Http.StringContent 日期格式