incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brian Candler <>
Subject Some CouchDB Qs & As
Date Sun, 25 Jan 2009 12:09:50 GMT
As a newbie, I had some questions which I still had after reading the wiki
and the book so far, so I thought I'd do some experiments to find the

I'm just posting them here in case it's useful to anyone else, or it could
provide some ideas when writing the book.

(1a) Does CouchDB store the raw JSON which it receives, character by
character, or does it convert to and from an internal representation?
(1b) Does CouchDB accept valid Javascript which is not valid as JSON, e.g.
{foo:"bar"} or {foo:/bar/} ?

Let's test 1b first:

$ cat test.dat
$ curl -T test.dat

So the answer is "no", only strict JSON is allowed. Now to try 1a:

$ cat test.dat
$ curl -T test.dat
$ curl

This suggests that the JSON is converted into some internal representation,
and then converted back to JSON.

(2a) When PUTting an identical version of a document, does CouchDB still
allocate a new _rev?
(2b) What about when PUTting a document which is semantically identical
JSON, but differs in ordering of object members?

Let's try 2a first:

$ cat test2.dat
  "foo": "value1",
  "bar": "value2"
$ curl -T test2.dat
$ curl >test2a
$ cat test2a
$ curl -T test2a
$ curl >test2b
$ cat test2b

So it seems the answer is: a new _rev is allocated for any PUT, even if the
uploaded data is identical JSON. Hence 2b is irrelevant. It is up to the
client only to determine whether the data has changed or not, before
invoking a PUT operation.

(3) It is documented (but not stressed) that a document is a JSON object, as
opposed to any JSON value, but I thought I'd check that too:

$ cat test3.dat
$ curl -T test3.dat

(4) If a document has attachments, what happens if you upload a new version
without the _attachments member or not listing a particular attachment? Is
it treated as an error, or ignored, or are the missing attachment(s) removed?

$ cat test4.dat
$ curl -T test4.dat
$ curl
This is foo
$ curl
and this is bar
$ curl >test4a
$ cat test4a
$ cp test4a test4b
$ vi test4b
... remove bar.txt attachment ...
$ cat test4b
$ curl -T test4b
$ curl
$ curl
This is foo
$ curl
{"error":"not_found","reason":"Document is missing attachment"}

So this suggests that omitting the _attachment entry when PUTing will
delete the attachment.

OK, what about if you submit with a new content_type and/or length?

$ cat test4d
$ curl -T test4d
$ curl
$ curl
This is foo

So it looks like the attributes of the attachment are ignored. (This begs
the question: is it possible to change the content_type of an attachment
without re-uploading it? But that's probably not very useful anyway)

(5) How do the document's _id attribute and the id given in the URL
interact? Specifically:
(5a) If I PUT a new document to /db/id1 but the document contains
"_id":"id2", which wins?

$ cat test5a.dat
$ curl -T test5a.dat

Answer: The _id attribute is ignored, and the URL wins

(5b) What about for an existing document?

$ cat test5b
$ curl -T test5b

Answer: The _id attribute is ignored, and the URL wins. (But clearly the
_rev is taken from the document itself)

(5c) If I POST to /db but the document contains "_id":"id3", is a random
document id still assigned?

$ curl -d '{"_id":"xxxyyy", "foo":"bar"}'

Answer: Yes, the document's _id attribute is ignored.

(5d) What about _rev?

For PUT:
$ curl >test5c
$ curl -T test5c
{"error":"conflict","reason":"Document update conflict."}

Answer: The presence of _rev indicates whether the document already exists
or not, so whilst _id is ignored, _rev must be removed if you are going to
make a copy of an existing document.

$ curl |
  curl -X POST -T -

Answer: for POST, both _id and _rev are ignored.

Aside: I see at

  "A CouchDB document is simply a JSON object ... The document can be an
  arbitrary JSON object"

which technically answers (1b) and (3). However in the book I suggest it may
be worth discussing exactly what is a valid CouchDB document, and what is



View raw message