database - 在 Mongo 聚合中对多个对象数组进行排序
问题描述
{
"_id" : 1,
"name" : "Ashwanth Madhav",
"scores" : [
{
"type" : "exam",
"score" : 45
},
{
"type" : "quiz",
"score" : 34
},
{
"type" : "work",
"score" : 99
},
{
"type" : "work",
"score" : 38
}
],
"job_type" : [
{
"type" : "full_time",
"activity" :87
},
{
"type" : "part_time",
"activity" : 78
},
{
"type" : "full_time",
"activity" : 71.76133439165544
}
]
}
使用$addToSet
排序列表时,会变得未排序。所以我用$push
. 但它会导致数据重复。
我需要对 SCORES 和 JOB_TYPE 进行排序。现在,我已经完成了,$addToSet
首先使用,然后是 " $unwind
" score-> " $sort
" -> " $group
" with $push
。在“ $unwind
”作业类型->“ $sort
”->“ $group
”之后加上$push
.
有没有更好的办法?提前致谢。
解决方案
推荐阅读
- node.js - 如果有共享卷,节点应用程序将不会在 Docker 中启动
- javascript - Sinon - 我可以模拟构造函数吗?
- javascript - 测试Edge中是否存在框架
- python - Python - 使用输入参数链接生成器
- reactjs - index.js:1 警告:收到非布尔属性“活动”的“真”
- reactjs - 如果没有管理员角色,如何在 Nextjs 中保护客户端的路由并重定向?
- visual-studio-code - 编辑 VSCode JSON 设置而不是 Gui 设置
- reactjs - 你如何构建一个网络应用程序?
- r - 删除绘图中的自动标题 (R)
- reactjs - 无法从 API 中提取数据并在 React 中绘制数据