Created
August 18, 2010 08:16
-
-
Save breun/533964 to your computer and use it in GitHub Desktop.
Update all documents from a view in CouchDB.
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
#!/usr/bin/env python | |
# | |
# This script updates a field for all documents in a view. Modify as needed. | |
# | |
# This script requires couchdbkit: | |
# $ sudo easy_install -U Couchdbkit | |
from couchdbkit import Server | |
import logging | |
# Configuration | |
server_url = "http://example.com:5984" | |
database = "database" | |
view = "design_name/view_name" | |
field = "field_name" | |
old_value = "old_value" | |
new_value = "new_value" | |
# Program | |
logging.basicConfig(level=logging.INFO) | |
server = Server(server_url) | |
db = server.get_or_create_db(database) | |
entries_to_process = db.view(view, key=old_value, reduce=False, include_docs=True) | |
for entry in entries_to_process: | |
doc = entry["doc"] | |
doc[field] = new_value | |
db.save_doc(doc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment