Created
February 6, 2013 20:59
-
-
Save wladston/4725777 to your computer and use it in GitHub Desktop.
Strip all strings that exist in a MongoDB database.
It will work recursively to strip **ALL** your strings. Binary data won't be affected.
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
# -*- coding: utf-8; -*- | |
from bson import Binary | |
from pymongo import Connection | |
from pymongo.database import Database | |
db = Database(Connection(), "yourdb") | |
def _clean_dict(x): | |
dic = {} | |
def clean_elem(elem): | |
if isinstance(elem, basestring) and not isinstance(elem, Binary): | |
return elem.strip() | |
if isinstance(elem, list): | |
return map(clean_elem, elem) | |
if isinstance(elem, dict): | |
return _clean_dict(elem) | |
return elem | |
for key, value in x.items(): | |
dic[key] = clean_elem(value) | |
return dic | |
count = 0 | |
for col in db.collection_names(): | |
for doc in getattr(db, col).find({}): | |
clean = _clean_dict(doc) | |
if clean != doc: | |
count += 1 | |
getattr(db, col).update({'_id': clean['_id']}, clean, multi=0) | |
print "%d documents changed." % count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment