Skip to content

Instantly share code, notes, and snippets.

@alejandrobernardis
Created January 16, 2014 16:47
Show Gist options
  • Save alejandrobernardis/8458448 to your computer and use it in GitHub Desktop.
Save alejandrobernardis/8458448 to your computer and use it in GitHub Desktop.
Mongo DB ops
#!/bin/bash
REPORT_DATE=$(date +%Y%m%dT%H%M%S)
REPORT_PATH=`pwd`/r${REPORT_DATE}
# Clean
rm -fR ${REPORT_PATH}
rm -f *.json
mkdir -p ${REPORT_PATH}
# Drop DB
mongo localhost:27017/c8 --eval 'printjson(db.dropDatabase())'
# devices
[ ! -e devices.bson ] && echo 'E: devices.bson not found' && exit 1
bsondump devices.bson > devices.json
mongoimport --db c8 --collection devices --file devices.json
mongo --shell localhost:27017/c8 devices.ops.js
mongoexport --csv --db c8 --collection report.devices --out ${REPORT_PATH}/report.devices.csv --fields _id.year,_id.month,_id.day,total
mongoexport --csv --db c8 --collection report.devicestypesbyday --out ${REPORT_PATH}/report.devices.types.by.day.csv --fields _id.date.year,_id.date.month,_id.date.day,_id.device,total
mongoexport --csv --db c8 --collection report.devicestypes --out ${REPORT_PATH}/report.devices.types.csv --fields _id,total
mongoexport --csv --db c8 --collection devices --out ${REPORT_PATH}/devices.csv --fields _id,device,level,points,balance,created,last_login
# profiles
[ ! -e profiles.bson ] && echo 'E: profiles.bson not found' && exit 1
bsondump profiles.bson > profiles.json
mongoimport --db c8 --collection profiles --file profiles.json
mongo --shell localhost:27017/c8 profiles.ops.js
mongoexport --csv --db c8 --collection report.profiles --out ${REPORT_PATH}/report.profiles.csv --fields _id.year,_id.month,_id.day,total
mongoexport --csv --db c8 --collection profiles --out ${REPORT_PATH}/profiles.csv --fields _id,level,points,balance,created,last_login
# tar
tar cfPvz r${REPORT_DATE}.tar.gz ${REPORT_PATH}
op=db.devices.aggregate([{$project:{date:1,newDate:{year:{$year:'$created'},month:{$month:'$created'},day:{$dayOfMonth:'$created'},}}},{$group:{_id:'$newDate',total:{$sum:1}}},{$sort:{_id:-1}},]);
db.report.devices.insert(op.result);
op=db.devices.aggregate([{$project:{date:1,newDate:{year:{$year:'$created'},month:{$month:'$created'},day:{$dayOfMonth:'$created'},},device:"$device"}},{$group:{_id:{date:'$newDate',device:'$device'},total:{$sum:1}}},{$sort:{_id:-1}},]);
db.report.devicestypesbyday.insert(op.result);
op=db.devices.aggregate([{$group:{_id:'$device',total:{$sum:1}}},{$sort:{_id:-1}},]);
db.report.devicestypes.insert(op.result);
exit;
op=db.profiles.aggregate([{$project:{date:1,newDate:{year:{$year:'$created'},month:{$month:'$created'},day:{$dayOfMonth:'$created'},}}},{$group:{_id:'$newDate',total:{$sum:1}}},{$sort:{_id:-1}},]);
db.report.profiles.insert(op.result);
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment