Created
November 28, 2011 14:20
-
-
Save retrofox/1400561 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * MongoDB script that attempts to remove bastards calendars | |
| * A calendar is a bastard calendar when: | |
| * - has an ObjectId _classroom property | |
| * - Classroom Object with that _classroom ObjectId() no longer exists. | |
| */ | |
| var calendars = db.calendars.find({ _classroom: { $ne: null }}) | |
| , i = 0 | |
| , l = calendars.count() | |
| , bastardsEventsDontMatter = false; | |
| print("\nProcessing " + l + " calendars with _classroom fields.\n"); | |
| calendars.forEach(function (calendar) { | |
| i++; | |
| //printjson(calendar); | |
| var classroom = db.classrooms.findOne({ _id: calendar._classroom }); | |
| if (!classroom) { | |
| print(i + " -\t'" + calendar.name + "' calendar is a fucking bastard !"); | |
| var calevents = db.calevents.find({ _calendar: calendar._id }) | |
| , countEvents = calevents.count(); | |
| if (countEvents) { | |
| print('\tbut has ' + countEvents + ' events.'); | |
| if (bastardsEventsDontMatter) { | |
| print('\tanyway ... removing calendar'); | |
| db.calendars.remove({ _id: calendar._id }); | |
| } | |
| else { | |
| print('\tNot removed. Disassociating the Calendar with the inexistent Classroom'); | |
| db.calendars.update({ _id: calendar._id }, { $set: { _classroom: null } }); | |
| } | |
| } | |
| else { | |
| print('\tremoving calendar'); | |
| db.calendars.remove({ _id: calendar._id }); | |
| } | |
| } | |
| checkError(); | |
| }); | |
| function checkError () { | |
| var err = db.getLastError(); | |
| if (err) { | |
| print('Error updating calendar: ' + err); | |
| print('Updated ' + i + ' calendars before failure'); | |
| exit(1); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment