假设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根据子项中的指标查找最小或最大值的更多相关文章

  1. POJ 3273 Monthly Expense二分查找[最小化最大值问题]

    POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...

  2. 【Python实践-5】使用迭代查找一个list中最小和最大值

    # -*- coding: utf-8 -*- #使用迭代查找一个list中最小和最大值,并返回一个tuple #遍历list,找到最小值 def findMinAndMax(L): if L==[] ...

  3. 请使用迭代查找一个list中最小和最大值,并返回一个tuple

    如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration). 在Python中,迭代是通过for ... in来完成的,而很多语 ...

  4. python 练习题:使用迭代查找一个list中最小和最大值,并返回一个tuple

    # -*- coding: utf-8 -*- # 请使用迭代查找一个list中最小和最大值,并返回一个tuple from collections import Iterable def findM ...

  5. 原创:从海量数据中查找出前k个最小或最大值的算法(java)

    现在有这么一道题目:要求从多个的数据中查找出前K个最小或最大值 分析:有多种方案可以实现.一.最容易想到的是先对数据快速排序,然后输出前k个数字.   二.先定义容量为k的数组,从源数据中取出前k个填 ...

  6. python 请使用迭代查找一个list中最小和最大值,并返回一个tuple

    请使用迭代查找一个list中最小和最大值,并返回一个tuple: 要注意返回的值的类型是不是tuple def findMinAndMax(L): min=0 max=0 if len(L)==0: ...

  7. 【编程题目】查找最小的 k 个元素

    5.查找最小的 k 个元素(数组)题目:输入 n 个整数,输出其中最小的 k 个.例如输入 1,2,3,4,5,6,7 和 8 这 8 个数字,则最小的 4 个数字为 1,2,3 和 4. 算法里面学 ...

  8. sql查找最小缺失值与重用被删除的键(转载)

    转载自:http://blog.csdn.net/yanghua_kobe/article/details/6262550 在数据处理时,我们经常会使用一些“自增”的插入方式来处理数据.比如学生学号: ...

  9. Oracle Statspack报告中各项指标含义详解~~学习性能必看!!!

    Oracle Statspack报告中各项指标含义详解~~学习性能必看!!! Data Buffer Hit Ratio#<#90# 数据块在数据缓冲区中的命中率,通常应该在90%以上,否则考虑 ...

随机推荐

  1. redis和memcached选择,对比分析

    memcache和redis是互联网分层架构中,最常用的KV缓存.不少同学在选型的时候会纠结,到底是选择memcache还是redis? memcache提供的功能是redis提供的功能的子集,不用想 ...

  2. python selenium-1 环境搭建new

    1.Mac安装Anaconda集成环境 集成环境有助于快速编写脚本. 将安装的python3版本设置为python,系统的python更改为python2 Anaconda地址 https://www ...

  3. java study2

    Intellj小技巧 数组 1.java数组元素类型是唯一的,即一个数组只能存储一种数据类型的数据,而不能存储多种数据类型的数据. 2.java数组的长度,一旦初始化完成,控件就被固定,即数组的长度将 ...

  4. 跨域获取json数据

    原文地址:http://my.oschina.net/LinBandit/blog/34570   前阵子做了一个前端动态加载json数据的应用,其中使用xmlhttprequest动态加载js,但是 ...

  5. nginx 限流配置

    上配置 http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr ...

  6. 1041 Be Unique (20 分)

    1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...

  7. linux查看某个目录下有哪些文件的命令

    分别是ll和ls命令 ll /usr/local/lib ls /usr/local/lib

  8. 微软SMB 3.0文件共享协议新特性介绍

    SMB(*nix平台和Win NT4.0又称CIFS)协议是Windows平台标准文件共享协议.Linux平台通过samba来支持.SMB最新版本v3.0,在v2.0基础上针对WAN和分布式有改进.详 ...

  9. 使用promisify来流程化异步操作

    现代js包括nodejs中有很多函数都是异步执行的, 我们总是需要写一个回调函数并且作为最后以一个参数传入,而我希望的是能像写promise这样的回调 promise .then() .then() ...

  10. License分类 + 引入开源软件时License的注意事项

    License分类 GPL: linux.openJDK,openJFX,mysql 融合感染,单独子模块不感染(自己的模块与引入模块的通信方式是socket) openJDK(GNU General ...