Skip to content

Instantly share code, notes, and snippets.

@travist
Last active March 29, 2022 13:52
Show Gist options
  • Select an option

  • Save travist/495ea81e18158b88a8044e7bb1b93790 to your computer and use it in GitHub Desktop.

Select an option

Save travist/495ea81e18158b88a8044e7bb1b93790 to your computer and use it in GitHub Desktop.
Delete a Project within MongoDB, and all MongoDB documents within that Project
db.projects.find({_id:ObjectId('5c77ef689ea802484df52353')}).forEach(function(project) {
print('Deleting the following project and all resources.');
printjson(project);
db.forms.find({project: project._id}).forEach(function(form) {
print('Deleting form ' + form._id);
print('Deleting form(' + form._id + ') submissions');
db.submissions.deleteMany({form: form._id});
print('Deleting form(' + form._id + ') actions');
db.actions.deleteMany({form: form._id});
print('Deleting form(' + form._id + ') revisions');
db.formrevisions.deleteMany({_rid: form._id});
db.forms.deleteOne({_id: form._id});
});
db.roles.find({project: project._id}).forEach(function(role) {
print('Deleting role(' + role._id + ')');
db.roles.deleteOne({_id: role._id});
});
db.tags.find({project: project._id}).forEach(function(tag) {
print('Deleting project tag ' + tag._id);
db.tags.deleteOne({_id: tag._id});
});
db.projects.deleteOne({_id: project._id});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment