Skip to content

Commit

Permalink
Scripts to export data from cloud database
Browse files Browse the repository at this point in the history
  • Loading branch information
Olliebrown committed Dec 15, 2021
1 parent 93acd09 commit 19f0551
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
125 changes: 125 additions & 0 deletions Server/dataExport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import MongoDB from 'mongodb'
import assert from 'assert'
import ArrayToCsv from 'arrays-to-csv'

const MongoClient = MongoDB.MongoClient

const agg = [{
$match: {}
}, {
$lookup: {
from: 'Users',
localField: 'userID',
foreignField: '_id',
as: 'user'
}
}, {
$addFields: {
teamID: {
$first: '$user.teams'
}
}
}, {
$addFields: {
date: {
$dateToString: {
date: '$timestamp',
format: '%m/%d/%Y %H:%M',
timezone: 'America/Chicago'
}
}
}
}, {
$lookup: {
from: 'Affects',
localField: 'affectID',
foreignField: '_id',
as: 'affect'
}
}, {
$lookup: {
from: 'Teams',
localField: 'teamID',
foreignField: '_id',
as: 'team'
}
}, {
$addFields: {
teams: {
$reduce: {
input: '$team.name',
initialValue: '',
in: {
$cond: [{
$eq: [
'$$value',
''
]
}, {
$concat: [
'$$this'
]
}, {
$concat: [
'$$value',
', ',
'$$this'
]
}]
}
}
}
}
}, {
$project: {
_id: 0,
date: 1,
isPrivate: 1,
affect: {
$first: '$affect'
},
userName: {
$first: '$user.name'
},
preferredName: {
$first: '$user.preferredName'
},
teams: 1
}
}, {
$project: {
date: 1,
isPrivate: 1,
affectName: '$affect.name',
affectEmoji: {
$first: '$affect.characterCodes'
},
userName: 1,
preferredName: 1,
teams: 1
}
}, {
$sort: {
timestamp: 1
}
}]

MongoClient.connect(
'mongodb+srv://berriers:[email protected]/test?authSource=admin&replicaSet=atlas-18vq4h-shard-0&readPreference=primary&appname=MongoDB+Compass&ssl=true', {
useNewUrlParser: true,
useUnifiedTopology: true
},
function (connectErr, client) {
assert.equal(null, connectErr)
const coll = client.db('karunaData').collection('AffectHistory')
coll.aggregate(agg).toArray((err, results) => {
if (err) {
console.error(err)
} else {
const csvGenerator = new ArrayToCsv(results, { delimiter: ',', quote: '"' })
csvGenerator.saveFile('./data.csv')
}
client.close()
})
}
)
1 change: 1 addition & 0 deletions Server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"tiny-markdown-editor": "0.1.4"
},
"dependencies": {
"arrays-to-csv": "^2.0.1",
"aws4": "^1.11.0",
"bcrypt": "^5.0.1",
"chokidar": "^3.5.2",
Expand Down

0 comments on commit 19f0551

Please sign in to comment.