Skip to content

Instantly share code, notes, and snippets.

@chrismytton
Last active August 29, 2015 14:18
Show Gist options
  • Save chrismytton/8991474e2bfa6538a467 to your computer and use it in GitHub Desktop.
Save chrismytton/8991474e2bfa6538a467 to your computer and use it in GitHub Desktop.
Mongo scripts for fixing image ids on YNMP
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");
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