-
-
Save chrismytton/8991474e2bfa6538a467 to your computer and use it in GitHub Desktop.
Mongo scripts for fixing image ids on YNMP
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
var organizations = db.organizations.find(); | |
var re = new RegExp(/^http\:\/\/yournextmp.popit.mysociety.org\/organizations\/(\d+)\/image\/(\w+)/); | |
var needsSaving; | |
var updated = 0; | |
organizations.forEach(function(organization) { | |
if (organization.images) { | |
needsSaving = false; | |
organization.images.forEach(function(image) { | |
if (!(image._id instanceof ObjectId)) { | |
var result = re.exec(image.url); | |
if (result) { | |
image._id = ObjectId(result[2]); | |
} else { | |
image._id = new ObjectId(); | |
} | |
needsSaving = true; | |
} | |
}); | |
if (needsSaving) { | |
db.organizations.save(organization); | |
updated++; | |
} | |
} | |
}); | |
print("Updated", updated, "records"); |
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
var people = db.persons.find(); | |
var re = new RegExp(/^http\:\/\/yournextmp.popit.mysociety.org\/persons\/(\d+)\/image\/(\w+)/); | |
var needsSaving; | |
var updated = 0; | |
people.forEach(function(person) { | |
if (person.images) { | |
needsSaving = false; | |
person.images.forEach(function(image) { | |
if (!(image._id instanceof ObjectId)) { | |
var result = re.exec(image.url); | |
if (result) { | |
image._id = ObjectId(result[2]); | |
} else { | |
image._id = new ObjectId(); | |
} | |
needsSaving = true; | |
} | |
}); | |
if (needsSaving) { | |
db.persons.save(person); | |
updated++; | |
} | |
} | |
}); | |
print("Updated", updated, "records"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment