mongodb根据子项中的指标查找最小或最大值
假设students集合中有这样的数据:
{
"_id" : 1,
"name" : "Aurelia Menendez",
"scores" : [
{
"type" : "exam",
"score" : 60.06045071030959
},
{
"type" : "quiz",
"score" : 52.79790691903873
},
{
"type" : "homework",
"score" : 71.76133439165544
},
{
"type" : "homework",
"score" : 34.85718117893772
}
]
},
{
"_id" : 3,
"name" : "Bao Ziglar",
"scores" : [
{
"type" : "exam",
"score" : 71.64343899778332
},
{
"type" : "quiz",
"score" : 24.80221293650313
},
{
"type" : "homework",
"score" : 1.694720653897219
},
{
"type" : "homework",
"score" : 42.26147058804812
}
]
},
{
"_id" : 2,
"name" : "Corliss Zuk",
"scores" : [
{
"type" : "exam",
"score" : 67.03077096065002
},
{
"type" : "quiz",
"score" : 6.301851677835235
},
{
"type" : "homework",
"score" : 20.18160621941858
},
{
"type" : "homework",
"score" : 66.28344683278382
}
]
}
需要找出每个学生成绩最小值,可使用如下语句:
db.students.aggregate([{$project: {minScore: {$min: "$scores.score"}}}])
语句里面的minScore等于是一个别名,可以使用其它的。结果如下:
{ "_id" : 1, "minScore" : 34.85718117893772 }
{ "_id" : 3, "minScore" : 1.694720653897219 }
{ "_id" : 2, "minScore" : 6.301851677835235 }
{ "_id" : 4, "minScore" : 19.21886443577987 }
{ "_id" : 5, "minScore" : 10.53058536508186 }
{ "_id" : 7, "minScore" : 42.48780666956811 }
{ "_id" : 6, "minScore" : 16.58341639738951 }
{ "_id" : 8, "minScore" : 14.63969941335069 }
{ "_id" : 9, "minScore" : 12.47568017314781 }
{ "_id" : 0, "minScore" : 1.463179736705023 }
{ "_id" : 12, "minScore" : 14.78936520432093 }
{ "_id" : 13, "minScore" : 78.18795058912879 }
{ "_id" : 14, "minScore" : 13.66179556675781 }
{ "_id" : 11, "minScore" : 15.81264595052612 }
{ "_id" : 16, "minScore" : 7.772386442858281 }
{ "_id" : 15, "minScore" : 3.311794422000724 }
{ "_id" : 18, "minScore" : 62.12870233109035 }
{ "_id" : 17, "minScore" : 31.15090466987088 }
{ "_id" : 10, "minScore" : 19.31113429145131 }
{ "_id" : 19, "minScore" : 0.6578497966368002 }
mongodb根据子项中的指标查找最小或最大值的更多相关文章
- POJ 3273 Monthly Expense二分查找[最小化最大值问题]
POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...
- 【Python实践-5】使用迭代查找一个list中最小和最大值
# -*- coding: utf-8 -*- #使用迭代查找一个list中最小和最大值,并返回一个tuple #遍历list,找到最小值 def findMinAndMax(L): if L==[] ...
- 请使用迭代查找一个list中最小和最大值,并返回一个tuple
如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration). 在Python中,迭代是通过for ... in来完成的,而很多语 ...
- python 练习题:使用迭代查找一个list中最小和最大值,并返回一个tuple
# -*- coding: utf-8 -*- # 请使用迭代查找一个list中最小和最大值,并返回一个tuple from collections import Iterable def findM ...
- 原创:从海量数据中查找出前k个最小或最大值的算法(java)
现在有这么一道题目:要求从多个的数据中查找出前K个最小或最大值 分析:有多种方案可以实现.一.最容易想到的是先对数据快速排序,然后输出前k个数字. 二.先定义容量为k的数组,从源数据中取出前k个填 ...
- python 请使用迭代查找一个list中最小和最大值,并返回一个tuple
请使用迭代查找一个list中最小和最大值,并返回一个tuple: 要注意返回的值的类型是不是tuple def findMinAndMax(L): min=0 max=0 if len(L)==0: ...
- 【编程题目】查找最小的 k 个元素
5.查找最小的 k 个元素(数组)题目:输入 n 个整数,输出其中最小的 k 个.例如输入 1,2,3,4,5,6,7 和 8 这 8 个数字,则最小的 4 个数字为 1,2,3 和 4. 算法里面学 ...
- sql查找最小缺失值与重用被删除的键(转载)
转载自:http://blog.csdn.net/yanghua_kobe/article/details/6262550 在数据处理时,我们经常会使用一些“自增”的插入方式来处理数据.比如学生学号: ...
- Oracle Statspack报告中各项指标含义详解~~学习性能必看!!!
Oracle Statspack报告中各项指标含义详解~~学习性能必看!!! Data Buffer Hit Ratio#<#90# 数据块在数据缓冲区中的命中率,通常应该在90%以上,否则考虑 ...
随机推荐
- sql表设计
数据库实际上是系统逻辑在磁盘上的固化,是信息河流的蓄水池. 数据库的表应有如下类型 1)类表.配置表.作为业务逻辑基本的名字,状态的定义,作为构建逻辑世界的最基础框架,解释框架的框架. 特点,数据不会 ...
- 关于Jquery 插件开发,写的很清楚了。。。
转自:http://blog.jobbole.com/30550/ 本文由 伯乐在线 - 戴嘉华 翻译.未经许可,禁止转载!英文出处:Extraordinarythoughts.欢迎加入翻译小组. 如 ...
- phonegap 2.9 IOS Xcode 搭建环境
一:下载phoneGap2.9和安装Xcode5(目前最新版) 选择2.9是因为3.0以上坑爹版本编译神马的要在有网络情况. 二: 下载phonegap后解压到你的指定文件夹中,解压后找到create ...
- my sql 只展示 前10条数据的写法
select * from 表 where 条件 limit 10 这里想看多少条 limit 后面的数字就是多少
- CentOS 6.2图形界面允许root用户登录
测试环境中,很多操作需要root用户权限,为了提高测试效率,因此有时希望直接使用root用户登陆系统,但是CentOS的图形界面默认不允许root用户登陆,这是由于pam的限制,需要进行如下设定: 1 ...
- autocomplete 自动填充 combobox
目录(?)[-] autocomplete有两种 一种 是 jquery ui里的 autocomplete httpjqueryuicomautocomplete 另一种是 ASPNET AJAX ...
- python之列表操作
1.列表的增操作(四种) append(object):append object to end,directly used on list insert(index,object):insert o ...
- HAproxy目录分发
ServerA服务的数据查询接口:/api/v1/datas HAproxy地址10.66.222.333将ServerA添加转发规则添加到HAproxyhaproxy.cfg frontend ht ...
- python入门-操作列表
1 Python根据缩进来进行判断代码行与前一个代码行的关系 for name in names: print(name) names = ['baker','david','philp','rose ...
- 15.python操作mysql
导入包 from pymysql import* 1. 创建 Connection 连接 conn=conne(host='192.168.13.130',port=3306 ,database='' ...