Re: Re - How to add new row to existing document in couchDB Database .

1,403 views
Skip to first unread message

Bradley Meck

unread,
Jan 31, 2013, 10:30:06 AM1/31/13
to nod...@googlegroups.com
GET the document from CouchDB, change the row appropriately, PUT it back to CouchDB. All using HTTP. CouchDB will complain about merge problems if the _rev field does not match, if so, retry.

Mark Hahn

unread,
Jan 31, 2013, 4:48:15 PM1/31/13
to nodejs
I have an update handler that I use for all updates.  You just send the change to the handler and it reads the doc, updates it, and writes it back, all in one http request.  I can even update items nested in objects and arrays using a syntax like "/a/b/c":"newValue".  I also support deleting fields at any level by having a magic value of "__delete__me__".


On Thu, Jan 31, 2013 at 7:30 AM, Bradley Meck <bradle...@gmail.com> wrote:
GET the document from CouchDB, change the row appropriately, PUT it back to CouchDB. All using HTTP. CouchDB will complain about merge problems if the _rev field does not match, if so, retry.

--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Michael Jackson

unread,
Jan 31, 2013, 5:43:16 PM1/31/13
to nod...@googlegroups.com
I'd be happy if I were wrong, but I don't believe it's possible to do all that with one HTTP request in CouchDB. The read and the write require at least two requests, a GET and a PUT as Bradley suggests...

--
Michael Jackson
@mjackson

Mark Hahn

unread,
Jan 31, 2013, 5:55:06 PM1/31/13
to nodejs
I'd be happy if I were wrong

Then smile.  I've been using it for a year.

Mark Hahn

unread,
Jan 31, 2013, 5:57:31 PM1/31/13
to nodejs
BTW, I am only talking about updates, which is what the OP asked about.  The doc must already exist.

Mark Hahn

unread,
Jan 31, 2013, 6:06:27 PM1/31/13
to nodejs
Here is the update handler.  As you can see it does what I claimed.  Enjoy ...

update: (doc, req) ->
postData = JSON.parse req.body
if doc
for k, v of postData
hash = doc
if k[0] is '/'
parts = k.split '/'
for part in parts[1..-2] then hash = (hash[part] ?= {})
k = parts[-1..-1][0]
if v is '__delete__me__' then delete hash[k]
else hash[k] = v
[doc, JSON.stringify {doc, status: 'updated'}]
else
[null, JSON.stringify status: 'nodoc']

Michael Jackson

unread,
Jan 31, 2013, 7:55:21 PM1/31/13
to nod...@googlegroups.com
Thanks for sharing. :) I had assumed that the OP didn't still have the original document hanging around in memory, as your "doc" argument suggests. Hence the need to GET it before updating.

--
Michael Jackson
@mjackson

Mark Hahn

unread,
Jan 31, 2013, 8:02:50 PM1/31/13
to nodejs
The doc argument comes from couch, not me.  The req is what I provide.  Check the update handler docs.

Again, I can upgrade or delete any set of isolated fields, even nested, with one http request.  The doc only needs to exist in the DB, not my app.

Nuno Job

unread,
Feb 1, 2013, 4:30:35 AM2/1/13
to nod...@googlegroups.com
Hi,

If you want to use CouchDB read the book first. It takes 2 days, and then you get it, and then you don't loose time figuring it out forever. It is the simplest programming book you have ever read (http://guide.couchdb.org)

I wrote a blog post about "updating" documents, if you are interested:


I would use a library to access CouchDB: cradle, nano, whatever you fancy. These are more used and have fixed strange bugs, plus if you are a beginner its probably a good idea to stay away from internals first UNLESS the intent is to learn them. If that's the case and you have the time, then go ahead and write your own. Check out on npm how to publish it.

Happy hacking,
Nuno

Thanks,
Nuno

Mark Hahn

unread,
Feb 1, 2013, 1:30:24 PM2/1/13
to nodejs
+1 for nano.  It is simple and just works.


--

Michael Jackson

unread,
Feb 2, 2013, 1:17:20 AM2/2/13
to nod...@googlegroups.com
Ah, got it now. Thanks for reminding me about the update handler feature of couch. I had totally forgotten about it.

--
Michael Jackson
@mjackson
Reply all
Reply to author
Forward
0 new messages