I am trying to update an already existing document by ID. My intention is to find the doc by its id, then change its "firstName" with new value coming in "json", then update it into the CouchDB database. Here is my code:

def updateDoc(self, id, json):
    doc = self.db.get(id)
    doc["firstName"] = json["firstName"]
    doc_id, doc_rev = self.db.save(doc)
    print doc_id, doc_rev
    print "Saved"
//"json" is retrieved from PUT request (request.json)

at self.db.save(doc) I'm getting exception as "too many values to unpack".

I am using Bottle framework, Python 2.7 and Couch Query.

How do I update the document by id? what is the right way to do it?

有帮助吗?

解决方案

In couchdb-python the db.save(doc) method returns tuple of _id and _rev. You're using couch-query - a bit different project that also has a db.save(doc) method, but it returns a different result. So your code should look like this:

def updateDoc(self, id, json):
    doc = self.db.get(id)
    doc["firstName"] = json["firstName"]
    doc = self.db.save(doc)
    print doc['_id'], doc['_rev']
    print "Saved"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top