1- const { MongoClient } = require ( "mongodb" ) ;
1+ require ( 'dotenv' ) . config ( ) ;
2+ const { MongoClient } = require ( 'mongodb' ) ;
3+ const fs = require ( 'fs' ) ;
4+ const csv = require ( 'csv-parser' ) ;
25
3- const uri = "mongodb+srv://daryna2003tk_db_user:[email protected] /dbWeek4?retryWrites=true&w=majority " ; 4- const client = new MongoClient ( uri ) ;
6+ const DB_NAME = "databaseWeek4 " ;
7+ const COLLECTION_NAME = "population" ;
58
6- async function totalPopulationByCountry ( countryName ) {
9+ async function importCSV ( ) {
10+ const client = new MongoClient ( process . env . MONGO_URI ) ;
711 try {
812 await client . connect ( ) ;
9- const db = client . db ( "dbWeek4" ) ;
10- const collection = db . collection ( "population" ) ;
13+ const db = client . db ( DB_NAME ) ;
14+ const collection = db . collection ( COLLECTION_NAME ) ;
15+
1116
12- const cursor = collection . find ( { Country : countryName } ) ;
13- const populationByYear = { } ;
17+ await collection . deleteMany ( { } ) ;
18+ console . log ( 'Collection cleared' ) ;
1419
15- await cursor . forEach ( doc => {
16- const year = doc . Year ;
17- const total = doc . M + doc . F ;
18- if ( ! populationByYear [ year ] ) populationByYear [ year ] = 0 ;
19- populationByYear [ year ] += total ;
20+ const results = [ ] ;
21+
22+ return new Promise ( ( resolve , reject ) => {
23+ fs . createReadStream ( 'population_pyramid_1950-2022.csv' )
24+ . pipe ( csv ( ) )
25+ . on ( 'data' , ( data ) => {
26+
27+ data . M = parseInt ( data . M ) ;
28+ data . F = parseInt ( data . F ) ;
29+ data . Year = parseInt ( data . Year ) ;
30+ results . push ( data ) ;
31+ } )
32+ . on ( 'end' , async ( ) => {
33+ try {
34+ if ( results . length > 0 ) {
35+ await collection . insertMany ( results ) ;
36+ console . log ( `Data imported successfully: ${ results . length } documents` ) ;
37+ }
38+ await client . close ( ) ;
39+ resolve ( ) ;
40+ } catch ( err ) {
41+ await client . close ( ) ;
42+ reject ( err ) ;
43+ }
44+ } )
45+ . on ( 'error' , async ( err ) => {
46+ await client . close ( ) ;
47+ reject ( err ) ;
48+ } ) ;
2049 } ) ;
50+ } catch ( err ) {
51+ console . error ( 'Import error:' , err ) ;
52+ await client . close ( ) ;
53+ throw err ;
54+ }
55+ }
2156
22- const result = Object . keys ( populationByYear ) . sort ( ) . map ( year => ( {
23- _id : parseInt ( year ) ,
24- countPopulation : populationByYear [ year ]
25- } ) ) ;
57+ async function totalPopulationByCountry ( countryName ) {
58+ const client = new MongoClient ( process . env . MONGO_URI ) ;
59+ try {
60+ await client . connect ( ) ;
61+ const db = client . db ( DB_NAME ) ;
62+ const collection = db . collection ( COLLECTION_NAME ) ;
63+
64+ const results = await collection . aggregate ( [
65+ { $match : { Country : countryName } } ,
66+ {
67+ $group : {
68+ _id : "$Year" ,
69+ countPopulation : { $sum : { $add : [ "$M" , "$F" ] } }
70+ }
71+ } ,
72+ { $sort : { _id : 1 } }
73+ ] ) . toArray ( ) ;
2674
27- console . log ( result ) ;
75+ console . log ( `\nTotal population for ${ countryName } :` ) ;
76+ console . log ( JSON . stringify ( results , null , 2 ) ) ;
77+ return results ;
2878
2979 } catch ( err ) {
30- console . error ( err ) ;
80+ console . error ( 'Error in totalPopulationByCountry:' , err ) ;
81+ return [ ] ;
3182 } finally {
32- client . close ( ) ;
83+ await client . close ( ) ;
84+ }
85+ }
86+
87+ async function continentPopulation ( yearValue , ageValue ) {
88+ const client = new MongoClient ( process . env . MONGO_URI ) ;
89+ try {
90+ await client . connect ( ) ;
91+ const db = client . db ( DB_NAME ) ;
92+ const collection = db . collection ( COLLECTION_NAME ) ;
93+
94+ const results = await collection . aggregate ( [
95+ {
96+ $match : {
97+ Year : yearValue ,
98+ Age : ageValue ,
99+ Country : {
100+ $in : [
101+ "AFRICA" ,
102+ "ASIA" ,
103+ "EUROPE" ,
104+ "LATIN AMERICA AND THE CARIBBEAN" ,
105+ "NORTHERN AMERICA" ,
106+ "OCEANIA"
107+ ]
108+ }
109+ }
110+ } ,
111+ {
112+ $project : {
113+ Country : 1 ,
114+ Year : 1 ,
115+ Age : 1 ,
116+ M : 1 ,
117+ F : 1 ,
118+ TotalPopulation : { $add : [ "$M" , "$F" ] }
119+ }
120+ }
121+ ] ) . toArray ( ) ;
122+
123+ console . log ( `\nContinent population for year ${ yearValue } , age ${ ageValue } :` ) ;
124+ console . log ( JSON . stringify ( results , null , 2 ) ) ;
125+ return results ;
126+
127+ } catch ( err ) {
128+ console . error ( 'Error in continentPopulation:' , err ) ;
129+ return [ ] ;
130+ } finally {
131+ await client . close ( ) ;
132+ }
133+ }
134+
135+ async function main ( ) {
136+ try {
137+ // await importCSV();
138+ await totalPopulationByCountry ( "Netherlands" ) ;
139+ await continentPopulation ( 2020 , "100+" ) ;
140+
141+ } catch ( err ) {
142+ console . error ( 'Main error:' , err ) ;
33143 }
34144}
35145
36- totalPopulationByCountry ( "Netherlands" ) ;
146+ main ( ) ;
0 commit comments