Mongodb聚合操作

参考

1 https://blog.csdn.net/vbirdbest/article/details/77102999
2 http://www.runoob.com/mongodb/mongodb-aggregate.html

我的代码


#!/usr/bin/env python
# -*- encoding: utf-8 -*-

import pymongo
import pandas as pd

# client = pymongo.MongoClient("")
client = pymongo.MongoClient("")
db = client.research
col = db.spotify_charts_data


def main():
    res = col.aggregate([
        {
            '$group': {
                '_id': {'country': '$country', 'date': '$date', 'streams': '$streams'},
                'date': {'$first': '$date'},
                'country': {'$first': '$country'},
                'count': {'$sum': '$streams'},
            }
        }
    ], allowDiskUse=True, )

    df = pd.DataFrame(list(res))
    df.to_excel('./spotify_count.xlsx')


if __name__ == '__main__':
    main()

猜你喜欢

转载自blog.csdn.net/qq_36653505/article/details/81539535
今日推荐