Skip to content

Instantly share code, notes, and snippets.

@ronfe
Created October 9, 2015 12:11
Show Gist options
  • Save ronfe/11121c02e285bc10aa09 to your computer and use it in GitHub Desktop.
Save ronfe/11121c02e285bc10aa09 to your computer and use it in GitHub Desktop.
# _*_ coding: utf8 _*_
__author__ = 'ronfe'
from pymongo import MongoClient
from bson.objectid import ObjectId
from collections import defaultdict, Counter
# Access db
dbClient = MongoClient('mongodb://10.8.8.8:27017')
db = dbClient['matrix-yangcong-prod25']
# Make school mapping dict
# Given province
provinces = db['provinces']
users = db['user']
shandongData = provinces.find_one({"name": '山东'})
cities = shandongData['cities']
output = {}
for each in cities:
cityName = each['name']
thisDistrict = each['districts']
for eachDistrict in thisDistrict:
distName = eachDistrict['name']
thisSchools = eachDistrict['schools']
for eachSchools in thisSchools:
output[eachSchools] = cityName + distName
schoolId = output.keys()
result = [
{
"_id" : ObjectId("54759f78560fdb1132ebd741"),
"count" : 2.0000000000000000
},
{
"_id" : ObjectId("54759f7c560fdb1132ebdbbc"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f7c560fdb1132ebdd06"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd75b"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f79560fdb1132ebd858"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd6c2"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd754"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd750"),
"count" : 29.0000000000000000
},
{
"_id" : ObjectId("54759f7b560fdb1132ebdb53"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd745"),
"count" : 2.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd743"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd74c"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd767"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f7e560fdb1132ebe042"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd740"),
"count" : 118.0000000000000000
},
{
"_id" : ObjectId("54759f77560fdb1132ebd5d3"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f77560fdb1132ebd576"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd744"),
"count" : 2.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd764"),
"count" : 3.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd74a"),
"count" : 1.0000000000000000
}
]
result2 = [
{
"_id" : ObjectId("54759f78560fdb1132ebd7b6"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd744"),
"count" : 3.0000000000000000
},
{
"_id" : ObjectId("54759f7c560fdb1132ebdd10"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f7c560fdb1132ebdc86"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd745"),
"count" : 3.0000000000000000
},
{
"_id" : ObjectId("54759f7b560fdb1132ebdb47"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd74c"),
"count" : 6.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd764"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f7b560fdb1132ebdb3c"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f77560fdb1132ebd592"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd750"),
"count" : 4.0000000000000000
},
{
"_id" : ObjectId("54759f7b560fdb1132ebdb53"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd749"),
"count" : 5.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd740"),
"count" : 74.0000000000000000
}
]
result3 = [
{
"_id" : ObjectId("54759f7c560fdb1132ebdc63"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f7a560fdb1132ebd939"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd764"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd75b"),
"count" : 2.0000000000000000
},
{
"_id" : ObjectId("54759f7a560fdb1132ebd93f"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd74d"),
"count" : 4.0000000000000000
},
{
"_id" : ObjectId("54759f77560fdb1132ebd569"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd6fb"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd749"),
"count" : 3.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd743"),
"count" : 4.0000000000000000
},
{
"_id" : ObjectId("54759f77560fdb1132ebd592"),
"count" : 19.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd746"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd759"),
"count" : 6.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd797"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f7c560fdb1132ebdd10"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd750"),
"count" : 20.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd7bc"),
"count" : 1.0000000000000000
},
{
"_id" : ObjectId("54759f78560fdb1132ebd740"),
"count" : 11.0000000000000000
},
{
"_id" : ObjectId("54759f7a560fdb1132ebd8ce"),
"count" : 1.0000000000000000
}
]
r = {}
for each in result:
eachSchool = each['_id']
eachDist = output[eachSchool]
each['count'] = int(each['count'])
registDist = r.keys()
if eachDist in registDist:
r[eachDist] += each['count']
else:
r[eachDist] = each['count']
for k in r:
print k + ': ' + str(r[k])
print '==============='
print ''
r = {}
for each in result2:
eachSchool = each['_id']
eachDist = output[eachSchool]
each['count'] = int(each['count'])
registDist = r.keys()
if eachDist in registDist:
r[eachDist] += each['count']
else:
r[eachDist] = each['count']
for k in r:
print k + ': ' + str(r[k])
print '==============='
print ''
r = {}
for each in result3:
eachSchool = each['_id']
eachDist = output[eachSchool]
each['count'] = int(each['count'])
registDist = r.keys()
if eachDist in registDist:
r[eachDist] += each['count']
else:
r[eachDist] = each['count']
for k in r:
print k + ': ' + str(r[k])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment