-
Notifications
You must be signed in to change notification settings - Fork 2
/
aggregate.js
67 lines (67 loc) · 2.55 KB
/
aggregate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
db.normalized.aggregate(
[
{ '$sort': { 'normalized.ein': 1, 'normalized.tax_period': -1, 'normalized.last_updated_irs': -1 } },
{ '$group': {
'_id': '$normalized.ein',
'last_updated_grantmakers': { '$first': '$normalized.last_updated_grantmakers'},
'last_updated_irs': { '$first': '$normalized.last_updated_irs'},
'ein': { '$first': '$normalized.ein' },
'organization_name': { '$first': '$normalized.organization_name' },
'names': { '$push': {'organization_name': '$normalized.organization_name' }},
'assets': { '$first': '$normalized.assets' },
'website': { '$first': '$normalized.website' },
'city': { '$first': '$normalized.city' },
'state': { '$first': '$normalized.state'},
'country': {'$first': '$normalized.country'},
'is_foreign': {'$first': '$normalized.is_foreign'},
'is_likely_staffed': { '$first': '$normalized.is_likely_staffed' },
'has_website': { '$first': '$normalized.has_website' },
'has_grants': { '$first': '$normalized.has_grants' },
'has_recent_grants': { '$first': '$normalized.has_recent_grants' },
'grant_max': { '$first': '$normalized.grant_max' },
'grant_min': { '$first': '$normalized.grant_min' },
'grant_median': { '$first': '$normalized.grant_median' },
'grant_count': { '$first': '$normalized.grant_count' },
'grant_count_all_years': { '$sum': '$normalized.grant_count' },
'grants_to_preselected_only': { '$first': '$normalized.grants_to_preselected_only' },
'filings': {
'$push': {
'object_id_irs': '$normalized.object_id_irs',
'tax_period': '$normalized.tax_period',
'tax_year': '$normalized.tax_year',
'url': '$normalized.url',
},
},
'grants': { '$first': '$normalized.grants'},
'people': { '$first': '$normalized.people'},
}},
{ '$project': {
'_id': 1,
'last_updated_grantmakers': 1,
'last_updated_irs': 1,
'ein': 1,
'organization_name': 1,
'organization_name_prior_year': { '$arrayElemAt': ['$names.organization_name', 1]},
'assets': 1,
'website': 1,
'city': 1,
'state': 1,
'country': 1,
'is_foreign': 1,
'is_likely_staffed': 1,
'has_website': 1,
'has_grants': 1,
'has_recent_grants': 1,
'grant_max': 1,
'grant_min': 1,
'grant_median': 1,
'grant_count': 1,
'grants_to_preselected_only': 1,
'filings': 1,
'grants': 1,
'people': 1,
}},
{ '$out': 'aggregated' },
],
{ 'allowDiskUse': true }
);