-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scripts to export data from cloud database
- Loading branch information
1 parent
93acd09
commit 19f0551
Showing
2 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters