Skip to content

Instantly share code, notes, and snippets.

@imtoanle
Created November 10, 2014 03:32
Show Gist options
  • Save imtoanle/27bcbb01c2bc99f8e7cc to your computer and use it in GitHub Desktop.
Save imtoanle/27bcbb01c2bc99f8e7cc to your computer and use it in GitHub Desktop.
// 1: create js script for mongoshell, file export_institutions.js
//print header csv file
print("provider_code,name,website,postal_address,phone_number,location_ids")
db.institutions.find().forEach(function(institution){
//map location_id
location_ids = "";
if (typeof institution.locations != 'undefined') {
institution.locations.forEach(function(location){
location_ids += location.location_id + " - ";
});
}
location_ids = location_ids.substring(0, location_ids.length - 3);
print('"' + institution.provider_code + '","' + institution.name + '","' +institution.website + '","' + institution.postal_address.address_line_1 + " - " + institution.postal_address.suburb + " - " + institution.postal_address.state + " - " + institution.postal_address.postcode + '","' + institution.contact_officers[0].phone + '","' + location_ids + '"');
});
// 2: run cmd
// mongo cricos_development export_institutions.js > out_institutions.csv
// similar with courses
// export_courses.js
print("course_name,course_code,duration,total_cost,course_level,location_ids")
db.courses.find().forEach(function(course){
location_ids = "";
if (typeof course.location_ids != 'undefined') {
course.location_ids.forEach(function(location_id){
location_ids += location_id + " - ";
});
}
location_ids = location_ids.substring(0, location_ids.length - 3);
print('"'+course.course_name + '","' + course.course_code + '","' +course.duration + '","' + course.total_cost + '","' + course.course_level + '","' + location_ids + '"');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment