1

As a CouchDB beginner I'm having a hard time understanding how to update documents.

When I read the docs I find this which is quite confusing for me:

1) Updating an Existing Document

To update an existing document you must specify the current revision number within the _rev parameter.

Source: Chapter 10.4.1 /db/doc

2) Update Functions

Update handlers are functions that clients can request to invoke server-side logic that will create or update a document.

Source: Chapter 6.1.4 Design Documents

Could you please tell me which way do you prefer to update your documents?


Edit 1:

Let's say the data structure is just a simple car document with some basic fields.

{
    "_id": "123",
    "name": "911",
    "brand": "Porsche",
    "maxHP": "100",
    "owner": "Lorna"
}

Now the owner changes, would you still use option 1? Option 1 has quite a downside, because I can't just edit one field. I need to retrieve every fields first, edit just the owner field and than send back the whole document. I just tried it and I find this quite long-winded. Hmmm...

Magiranu
  • 299
  • 4
  • 27

1 Answers1

2

Most of the time you want to choose option 1 "Update an existing document"; this operates on a standard document that stores data in the database. The other option relates to design documents, such as views (which are also documents, this is definitely confusing to new CouchDB users), which is something completely different.

Stick with option 1, and good luck :)

Lorna Mitchell
  • 1,819
  • 13
  • 22
  • Great explanation, thanks for that. May I ask if you would choose option 2 when you want to do partial updates on documents? I searched for that and only found answers which refer to using the update handler function. [Answer: 23208549](https://stackoverflow.com/a/23208549/8314623) & [Answer: 41485731](https://stackoverflow.com/a/41485731/8314623) for example. – Magiranu Aug 18 '17 at 08:42
  • 1
    You definitely can take that approach for partial updates but I'd be interested to see the data structure that requires it. I am firmly in the "more small documents are less likely to cause conflicts" camp although data design is, of course, a matter of taste. – Lorna Mitchell Aug 18 '17 at 09:55
  • Lorna, I allowed myself to edit my original question and added a simple example in regards to your comment. I would appreciate if you could tell me which option you would choose! Many thanks! – Magiranu Aug 18 '17 at 11:50
  • 1
    I'd still use option 1 (and thanks for assigning me an awesome car in your example!) – Lorna Mitchell Aug 18 '17 at 13:21