1. 程式人生 > >Mongo.aggregate

Mongo.aggregate

aggregate

聚合管道功能:

對文件進行過濾,查詢出符合條件的文件

對文件進行變換,改變文件的輸出形式

使用sanic_motor模組連線資料庫

from sanic import Sanic
from sanic.views import HTTPMethodView
from sanic.response import json, text, redirect, html
from sanic_motor import BaseModel

app = Sanic(__name__)

# 設定資料庫和集合
settings = {'MOTOR_URI': 'mongodb://localhost:27017/XXX', 'LOGO': None}
app.config.update(settings)
app.config.RESPONSE_TIMEOUT = 5*60
BaseModel.init_app(app)

class Subjectscope(BaseModel):
    __coll__ = 'XXX'

class Subject2020(HTTPMethodView):
    async def get(self, request):
        UniversityList = await Subjectscope.find(as_raw=True)
        RucodeList = []
        for Uni in UniversityList.objects:
            Rucode = Uni.get("Rucode", "")
            if Rucode not in RucodeList:
                RucodeList.append(Rucode)
            else:
                continue
        print(RucodeList)
        return json({"Rucode": "success"})
app.add_route(Subject2020.as_view(), 'rucodelist')

class SubjectAggregate(HTTPMethodView):
    async def get(self, request):
        my_pipeline =[
            # {"$skip":33440},
            {"$match":{"Rucode":{"$gte":'10001',"$lte":'10090'}}},
            # {"$group":{"_id":"$Rucode", "count":{"$sum":1}}}
            {"$project":{
                        "_id":0,
                        "_rucode":"$Rucode",
                        "SchoolName":1,
                        "ProfessionalType":1,
                        "typeid": {"$type":"$_id"},
                        "scopeMid":"$_id"}},
            {"$sort":{"_rucode":-1}},
            {"$limit":10},
            {"$sample":{"size":4}},
            # {"$group":{"_id":"$ProfessionalType","count":{"$sum":1}}}
            ]
        Myaggregate = await Subjectscope.aggregate(pipeline=my_pipeline)
        print(len(Myaggregate))
        return json(Myaggregate)
app.add_route(SubjectAggregate.as_view(), 'aggregate')
if __name__ == '__main__':
    app.run(host="127.0.0.1", port=8003, workers=1)
db.getCollection('subjectscope2020_benke').aggregate([
    {"$match":{"Rucode":{"$gt":'10001',"$lte":'10003'}}},
    //{"$project":{"_id":0, "Rucode":1, "SchoolName":1, "ProfessionalType":1}}
])

1.聚合管道

一、階段操作符

1.$project

提取(修改文件結構、重新命名、增加或刪除欄位)

db.getCollection('subjectscope2020_benke').aggregate([
{$project:
    {aid:{$type:"$_id"},brucode:{$type:"$Rucode"},cshcoolname:"$SchoolName"}
}
])
2.$match

篩選

3.$group

分組

$sum    計算總和。   db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}])
$avg    計算平均值   db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}])
$min    獲取集合中所有文件對應值得最小值。   db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}])
$max    獲取集合中所有文件對應值得最大值。   db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}])
$push   在結果文件中插入值到一個數組中。    db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}])
$addToSet   在結果文件中插入值到一個數組中,但不建立副本。 db.mycol.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}])
$first  根據資源文件的排序獲取第一個文件資料。 db.mycol.aggregate([{$group : {_id : "$by_user", first_url : {$first : "$url"}}}])
$last   根據資源文件的排序獲取最後一個文件資料 db.mycol.aggregate([{$group : {_id : "$by_user", last_url : {$last : "$url"}}}])
4.$sort

排序

5.$limit

限制

6.$skip

跳過

7.$unwind

拆分
每個文件包含陣列中的一個值

二、表示式操作符

1.布林管道聚合操作(Boolean Aggregation Operators)

$and $or $not

2.集合操作(Set Operators)
$setEquals 
$setIntersection 
$setUnion 
$serDifference 
$setIsSubset 
$anyElementTrue
$allElementsTrue 
3.比較聚合操作(Comparison Aggregation Operators)
$cmp
$eq
$gt
$gte
$lt
$lte
$ne
4.算術聚合操作(Arithmetic Aggregation Operators)
$abs
$add
$ceil
$divide
$exp
$floor
$ln
$log
$log10
$mod
$multiply
$pow
$sqrt
$subtract
$trunc
5.字串聚合操作(String Aggregation Operators)
$concat
$indexOfBytes
$indexOfCP
$split
$strLenBytes
$strLenCP
$strcasecmp
$substr
$substrBytes
$substrCP
$toLower
$toUpper
6.陣列聚合操作(Array Aggregation Operators)
$arrayElemAt
$concatArrays
$filter
$indexOfArray
$isArray
$range
$reverseArray
$reduce
$size
$slice
$zip
$in
7.日期聚合操作(Date Aggregation Operators)
$dayOfYear
$dayOfMonth
$dayOfWeek
$year
$month
$week
$hour
$minute
$second
$millisecond
$dateToString
$isoDayOfWeek
$isoWeek
$isoWeekYear
8.條件聚合操作(Conditional Aggregation Operators)
$cond
$ifNull
$switch
9.資料型別聚合操作(Data Type Aggregation Operators)
$type
10.其他
//$exists 存在
db.person.find( { phone: { $exists: true } } )

2.單目的聚合操作

"SS_Class_Info":{"$exists":true}

distinct("name":{"$exists":true})

去重

count()

返回文件個數

# 求出集合 article 中 time 值大於 2017-04-09 的文件個數
db.article.count({time:{$gt:newDate('04/09/2017')}})
# 這個語句等價於
db.article.find({time:{$gt:newDate('04/09/2017')}}).count()

db.getCollection('SS_SeleCourseResult').aggregate([
{"$match":{"Choose_Subject_Task_ID" : "5c106c08705deb0862d3bfa9"}},
{"$match":{"result":{"$elemMatch":{"Subject":"物理","Status":1,"SS_Class_Info":{"$exists":true},"SS_Class_Info.Class_Name":"物理選考2班"}}}},
{"$unwind":"$result"},
{"$match":{"result.Subject":"","result.Status":3}},
{"$group":{"_id":"$result.SS_Class_Info.Class_Name","studentlist":{"$push":"$UserStudent_Name"}}}
])
db.getCollection('sort_SubjectSCope2020').aggregate([
{"$match":{"Rucode":{"$gte":"10001","$lt":"10002"}}},
{"$unwind":"$ScopeArea"},
{"$match":{"ScopeArea.Province":"浙江省",}},
{"$unwind":"$ProfessionalName"},
{"$project":{"_id":0,"SchoolName":1,"ProfessionalType":1,"ProfessionalName":1,"Scope":1}},
{"$group":{"_id":"$Scope","count":{"$sum":1},"pfnamelist":{"$push":"$ProfessionalName"}}},
])