Skip to content

Instantly share code, notes, and snippets.

@isc-rsingh
Last active July 26, 2016 16:26
Show Gist options
  • Save isc-rsingh/a0cf7afc4e1c901734580471b04c02e1 to your computer and use it in GitHub Desktop.
Save isc-rsingh/a0cf7afc4e1c901734580471b04c02e1 to your computer and use it in GitHub Desktop.
Loop through all docs in a Cloudant database and update data. This version changes string values to integers but it can easily be adapted for other uses.
import os
from cloudant import cloudant
from cloudant.client import Cloudant
from cloudant.document import Document
CLOUDANT_USERNAME = os.getenv('CLOUDANT_USERNAME', 'myusername')
CLOUDANT_PW = os.getenv('CLOUDANT_PW', 'mypw')
CLOUDANT_DB = 'mydb'
PROPS = ['rec','r','h','m','s','idts','idvc','idn','refts']
with cloudant(CLOUDANT_USERNAME, CLOUDANT_PW, account=CLOUDANT_USERNAME) as client:
mcdb = client[CLOUDANT_DB]
for doc in mcdb:
print("Checking " + doc['_id'] + " viewts: "+str(doc['viewts']))
if type(doc['viewts']) is unicode:
print("Got a bad one.")
for prop in PROPS:
if ( type(doc[prop] is str or type(doc[prop]) is unicode) ):
if ( doc[prop] == 'null' ):
doc[prop] = None
else:
doc[prop] = int(doc[prop])
doc.save();
print("Saved "+doc['_id'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment