MongoDB finds the minimum or maximum value based on the metrics in the subkey

Source: Internet
Author: User
Tags mongodb

Suppose you have such data in the students collection:

{"_id": 1, "name": "Aurelia Menendez", "scores": [{"Type" : "Exam", "Score": 60.06045071030959}, {"T                        Ype ":" 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": "Homewor                     K ",   "Score": 1.694720653897219}, {"type": "Homework",        "Score": 42.26147058804812}]},{"_id": 2, "name": "Corliss Zuk",                 "Scores": [{"type": "Exam", "Score": 67.03077096065002 }, {"type": "Quiz", "Score": 6.30185167783 5235}, {"type": "Homework", "score": 20.18  160621941858}, {"type": "Homework", "score" : 66.28344683278382}]}

To find out the minimum value of each student's performance, use the following statement:

db.students.aggregate([{$project: {minScore: {$min: "$scores.score"}}}])

The statement inside minScore is equal to an alias and can be used for other. The results are as follows:

{ "_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 finds the minimum or maximum value based on the metrics in the subkey

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.