overwrite document without revision?

1,873 views
Skip to first unread message

Alex Ramos

unread,
Nov 8, 2013, 8:57:35 AM11/8/13
to us...@couchdb.apache.org
Is there any way to unconditionally overwrite a document without first
making a round-trip to get the current rev?

I'm trying to use CouchDB as a data warehouse of sorts and I just need to
overwrite docs with data coming from the transactional system of record (in
MySQL) when they change.

Stanley Iriele

unread,
Nov 8, 2013, 12:41:00 PM11/8/13
to us...@couchdb.apache.org
Update handlers sound like the way to go here....

Robert Newson

unread,
Nov 8, 2013, 1:33:48 PM11/8/13
to us...@couchdb.apache.org
There is not, not even update handlers can do that, by design.

B.

Daniel Nephin

unread,
Nov 8, 2013, 3:53:16 PM11/8/13
to us...@couchdb.apache.org
Isn't this the default behaviour?

All you need to update a document is it's '_id'. Since this is a data
warehouse, I assume the ID is already being set by the transaction system,
so you don't need to do a lookup. If you PUT a document (
http://docs.couchdb.org/en/latest/api/document/common.html#put--db-docid) using
it's ID, it will replace the previous revision.

Am I missing something?

Stanley Iriele

unread,
Nov 8, 2013, 4:39:12 PM11/8/13
to us...@couchdb.apache.org
I still think you can use update handlers for that...just send the doc over
the wire... Parse it and save it in the db with the current revision of the
current doc if it exists...you. Can't do this in bulk... And I think what
you're trying to Dave is the round trip... Which the update handler gives
you...

Stanley Iriele

unread,
Nov 8, 2013, 4:40:06 PM11/8/13
to us...@couchdb.apache.org
But no you can't just throw a doc in the db without the latest revision if
it already exists

Robert Newson

unread,
Nov 8, 2013, 4:42:09 PM11/8/13
to us...@couchdb.apache.org
Update handlers can still fail with a 409. :)

B.

Stanley Iriele

unread,
Nov 8, 2013, 4:44:33 PM11/8/13
to us...@couchdb.apache.org
Right right :-D..meh...I nice "while not =409 loop" should help with that...

Jens Alfke

unread,
Nov 8, 2013, 5:46:29 PM11/8/13
to us...@couchdb.apache.org

On Nov 8, 2013, at 12:53 PM, Daniel Nephin <dne...@gmail.com<mailto:dne...@gmail.com>> wrote:

Am I missing something?

Yes. To update a document you need its current _rev ID, not just the _id.

—Jens

Naik, Purushottam

unread,
Nov 8, 2013, 5:52:47 PM11/8/13
to us...@couchdb.apache.org
Hi Jens,
This (Yes) was sent to confirming to subscription to CouchDB! I am new to CouchDB. We are looking for help on professional training with CouchDB? Any good companies/experts to help with CouchDB development?
Thanks
PG

Purushottam G Naik (PG),
Senior Director iSeries Software,
Bally Technologies,
245 Town Park Drive,
5th Floor, Suite 500,
Town Park-Ravine One,
Kennesaw, GA 30144.
Direct 702-584-8899
Cell    404-903-0819
Main   770-420-2388
Fax    770-420-9650
pn...@ballytech.com
-Jens

Robert Newson

unread,
Nov 8, 2013, 6:25:54 PM11/8/13
to us...@couchdb.apache.org
Please start a new thread, don't tack unrelated items to existing ones.

B.

Dave Benson

unread,
Nov 8, 2013, 7:45:10 PM11/8/13
to us...@couchdb.apache.org
Couldn't you just store the _rev in your database?

If data is coming from a centralized server, there should be no chance of
conflicts. And if you do get conflicts, you ought to resolve them properly
(ie by making sure the latest revision in your main database and in couchdb
are the same).

Ryan Mohr

unread,
Nov 8, 2013, 10:07:38 PM11/8/13
to user
Couldn't you do this with _bulk_docs and new_edits:false?

http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API#Posting_Existing_Revisions

Jens Alfke

unread,
Nov 8, 2013, 11:25:23 PM11/8/13
to us...@couchdb.apache.org

On Nov 8, 2013, at 7:07 PM, Ryan Mohr <ry...@kumu.io> wrote:

> Couldn't you do this with _bulk_docs and new_edits:false?

This would add a new revision, but it'd be a new roots in the revision tree (i.e. a sibling not a child of the previous revision.) This would essentially create a conflict, and it would then be a toss-up which revision would be considered the current one. You don’t want to do this.

—Jens

Ryan Mohr

unread,
Nov 10, 2013, 4:36:16 PM11/10/13
to us...@couchdb.apache.org
Jens - Can you expand on this more or point me to documentation that covers
this in detail?

Alexander Shorin

unread,
Nov 10, 2013, 4:51:43 PM11/10/13
to us...@couchdb.apache.org
On Mon, Nov 11, 2013 at 1:36 AM, Ryan Mohr <ry...@kumu.io> wrote:
> Can you expand on this more or point me to documentation that covers
> this in detail?


From wiki link you posted:

> rather than being the parent revision ID to be matched against, it's the existing revision ID that will be saved as-is into the database.

It's easy to see:

[~]$ curl http://localhost:5984/db/foo
{
"_id": "foo",
"_rev": "3-00e7ae9eeb50cabd81126e9f39b871cf"
}

[~]$ curl -X PUT http://localhost:5984/db/foo -d
'{"bar":"baz","_rev":"1-ABC"}' -H "Content-Type:application/json"
{"error":"conflict","reason":"Document update conflict."}

[~]$ curl -X PUT 'http://localhost:5984/db/foo?new_edits=false' -d
'{"bar":"baz","_rev":"1-ABC"}' -H "Content-Type:application/json"
{"ok":true,"id":"foo","rev":"1-ABC"}

[~]$ curl -X PUT 'http://localhost:5984/db/foo?new_edits=false' -d
'{"bar":"baz","_rev":"9-CDE"}' -H "Content-Type:application/json"
{"ok":true,"id":"foo","rev":"9-CDE"}

[~]$ curl 'http://localhost:5984/db/foo?open_revs=all' -H
"Accept:application/json"
[
{
"ok": {
"_id": "foo",
"_rev": "9-CDE",
"bar": "baz"
}
},
{
"ok": {
"_id": "foo",
"_rev": "3-00e7ae9eeb50cabd81126e9f39b871cf"
}
},
{
"ok": {
"_id": "foo",
"_rev": "1-ABC",
"bar": "baz"
}
}
]



--
,,,^..^,,,

Dave Cottlehuber

unread,
Nov 10, 2013, 5:01:33 PM11/10/13
to us...@couchdb.apache.org
Hi Ryan,

You're desperately trying to find a way to make CouchDB's immutable
storage system work like a mutable one. Please don't :-) -- it's going
against the grain.

I'd suggest taking a step back for a moment, reading carefully through
these 2 sections in particular from the guide:

http://guide.couchdb.org/draft/consistency.html
http://guide.couchdb.org/draft/conflicts.html

What features are you after from your overall app that makes couchdb a
good fit (or not)?

What Jens is referring to is that new_edits is an API that allows the
multi-master replication engine to introduce conflicts directly into
the existing revision tree for a document. You'll then need to resolve
these introduced conflicts directly to ensure that your new entry
"comes out on top" as there's no guarantees it won't by default.

A+
Dave

Ryan Mohr

unread,
Nov 10, 2013, 6:47:31 PM11/10/13
to us...@couchdb.apache.org
Dave, this isn't my thread. Alex had asked:

> Is there any way to unconditionally overwrite a document without
first making a round-trip to get the current rev?

Everyone said no. Looks like it's totally doable using new_edits=false and
a script that resolves any conflicts by simply picking the most recent
update.

Whether or not couchdb is a good fit for Alex's needs is a separate issue.

Jens Alfke

unread,
Nov 13, 2013, 12:18:59 AM11/13/13
to us...@couchdb.apache.org

On Nov 10, 2013, at 3:47 PM, Ryan Mohr <ryan...@gmail.com> wrote:

> Everyone said no. Looks like it's totally doable using new_edits=false and
> a script that resolves any conflicts by simply picking the most recent
> update.

That’s a Rube Goldberg solution that’s more expensive than doing it the right way. Think of what that script needs to do.

In other words, it’s also totally doable using a simpler script that GETs the current revision, stores its _rev into the new document, and PUTs the new document.

—Jens

Ryan Mohr

unread,
Nov 13, 2013, 4:56:15 AM11/13/13
to user
Of course it is, but Alex asked can I not should I.
Reply all
Reply to author
Forward
0 new messages