Skip to content

Commit

Permalink
feat: cityStatistics script
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnl committed Oct 29, 2024
1 parent 07cc163 commit 9b0461e
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions scripts/cityStatistics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import "dotenv/config"
import database from "../src/database/database.js"

console.log("Connecting to database...")
await database.init()
.then(() => {
console.log("Database connected")
})
.catch((e) => {
console.error("Failed to connect to the database", e)
process.exit(1)
})

const data = await database.UserInfo.find(
{ "associationMembership.status": "MEMBER"},
{ "associationMembership.city": 1, _id: 0 }
);

const cities = data.map(item => item.associationMembership.city)

const cityCount = Object.fromEntries(
Object.entries(
cities.reduce((acc, city) => {
acc[city] = (acc[city] || 0) + 1;
return acc;
}, {})
).sort((a, b) => b[1] - a[1])
);

const resultString = Object.entries(cityCount)
.map(([city, count]) => `${count}\t${city}`)
.join('\n');

console.log(resultString)

0 comments on commit 9b0461e

Please sign in to comment.