zoukankan      html  css  js  c++  java
  • 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 }
    
  • 相关阅读:
    E3-1230和E3-1230 V2有多神?
    自己制作 SPx N合1 自动安装盘(x86)
    ARP防火墙绑定网关MAC地址预防ARP攻击和P2P终结者
    完全备份、差异备份和增量备份的权威解释!!!
    Win7 SP1语言包微软官方下载地址及使用方法 2
    Windows 7 不同安装模式简要区别(图解)
    Windows PE3.0制作方法(从Win7中提取制作)
    摄影初学者挑选相机的常见问题 FAQ
    提升域用户帐户在本地计算机上的权限
    Oracle重置序列
  • 原文地址:https://www.cnblogs.com/wuzhiblog/p/mongodb_min_max.html
Copyright © 2011-2022 走看看