So today I hacked together a simple view in a Python script. This returns all my documents with a .symbol of 'C'. I'm not good at JavaScript (yet) so this is about as advanced as I want to get as I focus on Python and CouchDB:
design_doc = {
'_id': '_design/test',
'language': 'javascript',
'views': {
'allcitis': {
"map": """function(doc) { if (doc.symbol == "C") { emit(doc._id, doc); }}"""
}
}
}
frenzydb.save_doc(design_doc)
No problem - worked the first time and I saw it my Cloudant database. Parsing the view results through couchdbkit to a little longer to figure out. The Getting Started page ends with:
greets = Greeting.view('greeting/all’)
It took me a little while to figure out 'greets' wasn't a list, it's a couchdbkit.client.ViewResults. As it says in the comments for couchdbkit/client.py, "It return an ViewResults object on which you could iterate, list, ...". Okay great, throw it in a list. Here's a trivial function to get the average of 'price' using the aforementioned view:
citis = list(frenzydb.view('/test/allcitis'))
sum = 0
for citi in citis:
sum += float(dict(citi['value'])['price'])
print round(sum / len(citis), 2)
No comments:
Post a Comment