Aggregate usage of MongoDB

Source: Internet
Author: User
Tags mongoclient python script

Construct some data with a Python script and then show how to use the

#coding =utf-8 ' Created on 2015-12-28 @author: kwsy ' ' Import pymongo client = Pymongo. Mongoclient ("localhost", 27017) db = client.testaggregate coll = db.teacher t1 = {' name ': ' Li ', ' Level ': 3, ' salary ': 10000, ' Stus ': [{' Name ': ' LiA ', ' class ': ' Language ', ' Socre ':}, {' name ': ' LiA ', ' class ': '
                                                   Math ', ' socre ': +}, {' name ': ' LiB ', ' class ': ' Language ', ' Socre ': 95},
                                                   {' name ': ' LiB ', ' class ': ' Math ', ' Socre ': 99}
                                                   ]} t2 = {' name ': ' Liu ', ' Level ': 2, ' salary ': 15000, ' stus ': [{' Name ': ' Liua ', ' class ': ' Language ', ' Socre ': 80},
                                                   {' name ': ' Liua ', ' class ': ' Math ', ' Socre ': 60}, {' name ': ' Liub ', ' class ': ' Language ', ' socre ': ' {' name ': ' Liub ', ' class ': ' Mathematics ', '
                                             Socre ': 90}      ]} t3 = {' name ': ' He ', ' Level ': 5, ' salary ': 20000, ' stus ': [{' Name ': ' HeA ', ' class ': ' Language ', ' Socre ': 96}, {' name ': ' HeA ', ' class ': ' Math ', ' socre ': 80}, { ' Name ': ' Heb ', ' class ': ' Chinese ', ' socre ': ' I ', {' name ': ' Heb ', ' class ': ' Math ', ' SOCR E ': $}]} T4 = {' name ': ' Black ', ' Level ': 5, ' salary ': 25000, ' stus ': [{' Name ': ' Blacka ', ' class ': ' Chinese ', ' socre ': ' {' name ': ' Blacka ', ' class ': ' Math ', ' Soc
                                                   Re ': i}, {' name ': ' blackb ', ' class ': ' Language ', ' Socre ': 66},
                                                   {' name ': ' blackb ', ' class ': ' Math ', ' socre ': 80} ]} coll.insert_one (t1) coll.insert_one (T2) Coll.insert_one (T3) coll.insert_one (T4)

The following script demonstrates how to use the

#coding =utf-8 ' Created on 2015-12-28 It is not a good idea to put students ' scores in the teacher's information document as embedded documents in this script, just for the sake of a more vivid explanation of the functions and effects of aggregation @author: Kwsy ' im Port Pymongo client = Pymongo. Mongoclient ("localhost", 27017) db = client.testaggregate coll = db.teacher #统计所有老师的工资总和, statistics are for teachers, so _id for empty print (U ' Statistic all teachers The sum of the wages ') data = Coll.aggregate ([{' $group ': {' _id '], ' ' count ': {' $sum ': 1}, ' Total_salary ': {' $sum ': ' $salary '}, ' avg ': ' $ Avg ': ' $salary '}}]) data = list (data) print (data[0]) #按照老师的级别统计工资和, with teacher-level statistics, _id is assigned to $level print (U ' statistics different levels of wages and ') data = List (coll.aggregate {' $group ': {' _id ': ' $level ', ' count ': {' $sum ': 1}, ' Total_salary ': {' $sum ': ' $salary '}, ' avg ': ' $ Avg ': ' $salary '}}])) for T in Data:print (t) #下面的语句等效于 find ({' name ': ' Black '}) pipeline = [{' $match ': {' name ': ' Black '}}] data = List (Coll.aggregate (pipeline)) print (data[0]) #使用了 $unwind enable inline documents to be upgraded to the previous level pipeline = [{' $match ': {' name ': ' Black '}},{' $unwind ': ' $stus '}] data = List (Coll.aggregate (pipeline)) for T in Data:print (t) # Upgrade after a match filter, filter only the score Embedded document in 80 pipeline = [{' $match': {' name ': ' Black '}},{' $unwind ': ' $stus '},{' $match ': {' stus.socre ': {' $gt ': {}}}] data = List (Coll.aggregate pipeline ) for T in Data:print (t) #保持原来的内嵌形式不变 print (U ' keep original inline form unchanged ') pipeline = [{' $match ': {' name ': ' Black '}},{' $unwind ': ' $ Stus '}, {' $match ': {' stus.socre ': {' $gt ': 80}}},{' $group ': {' _id ': ' $_id ', ' level ': {' $first ': ' $level '}, ' Stus ': {' $pus  H ': ' $stus '}}] data = List (Coll.aggregate (pipeline)) for T in Data:print (t) #所有学生的语文成绩 print (U ' language scores for all students ') pipeline

= [{' $unwind ': ' $stus '},{' $match ': {' stus.class ': ' Language '}}] data = List (Coll.aggregate (pipeline)) for T in Data:print (t) #所有学生的语文成绩排序取前三个 Print (U ' The first three of all students ' language scores pipeline = [{' $unwind ': ' $stus '},{' $match ': {' stus.class ': ' Language '}},{' $sort ') : {' stus.socre ': -1}},{' $limit ': 3}] data = List (Coll.aggregate (pipeline)) for T in Data:print (t) #某个老师的学生中语文成 Performance Max Print (U ' The maximum of Chinese performance in a teacher's student ") pipeline = [{' $match ': {' name ': ' Black '}},{' $unwind ': ' $stus '}, {' $match ': {' stus.c Lass ': ' Language '}},{' $sort ': {' stus.socre ':-1}}, {' $limit ': 1}] data = List (Coll.aggregate (pipeline)) for T in Data:print (t)
 

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.