0

I'm looking into couchDB. If i understand correctly, you can't just send a "query" to couchDB and do a partial update.

For example this document called "users" (simplified for clarity):

{
   allusers: [
      {"id": 1, "username":"myuser1", "pass":"secret"},
      {"id": 2, "username":"myuser2", "pass":"password"},
      {"id": 3, "username":"myuser3", "pass":"crypto"}
   ]
}

Is it true there isn't really a way to update user 3's password, that instead I have to load the entire document, make the update in javascript on the client, then send the whole object back using a PUT request?

I'm hoping that I just don't quite understand how couchDB works and wants things done.

How do I update one part of a possibly very large object?

tim
  • 3,823
  • 5
  • 34
  • 39

2 Answers2

4

Documents in a document store (like CouchDB is) are conceptually similar to rows in a RDBMS table or files in a filesystem, not tables.

In your example, you'd have seperate documents for myuser1, one for myuser2, and one for myuser3, each of which would be small, self-contained and therefore easy and fast to update. Ideally, all of your documents would be like that.

The CouchDB Guide has an excellent chapter on what CouchDB is and why (and when!) it's useful.

Wander Nauta
  • 18,832
  • 1
  • 45
  • 62
  • Ah! That's significant .Thank you. So if I have to make bulk updates, I end up sending a separate PUT request for each update? That could make a simple update on 10,000 documents a rather time consuming affair... right? – tim Apr 08 '15 at 21:09
  • 1
    Right, it wouldn't be great at that. You could use the [Bulk Document API](https://wiki.apache.org/couchdb/HTTP_Bulk_Document_API) to bundle some of the PUT requests together, or do the update on the server side using [update handlers](https://wiki.apache.org/couchdb/Document_Update_Handlers), but there's no `UPDATE` like in SQL. – Wander Nauta Apr 08 '15 at 21:19
1

Use an update handler function to avoid sending the whole doc to update partial parts.

You will have access to the data you have sent and the current version if the doc. Then you alter the doc and give it back to CouchDB.

Ingo Radatz
  • 1,225
  • 8
  • 9