You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Wordit Ltd <wo...@googlemail.com> on 2010/11/11 15:59:00 UTC

Update handler doc ids

Do update handlers require the doc id, or is it possible to search for
the doc as with views?

I need to update the document belonging to the logged in user. (I'll
use a validation function to check that.)
The only workaround I could think of is using the username as the doc
id. Then I call the update handler with .../handler/username?param=x

Marcus

Re: Update handler doc ids

Posted by Zachary Zolton <za...@gmail.com>.
> Thanks, got it working. This is good stuff.

Your welcome! Sorry, for the delayed response.

> Hmm, I don't follow. I have a doc for user foobar with "_id:foobar".
> If I had a second type of document, say one type with user's blog
> posts and another with comments by same user, then do you mean I'd
> need to add a field unique to usernames because the ID has already
> been used?

I'm saying that if you're going to use a natural key as the ID of a
document that you should namespace it. Like so:

{ "_id": "user:foo", "type": "user", "full_name": "Foo Baz Barington" }

That way, you can also have another kind of document, in a different
ID namespace, with the same natural key value. So, for instance, their
could be a book called Foo:

{ "_id": "book:foo", "type": "book", "title": "Foo" }

And their IDs wont collide.

Re: Update handler doc ids

Posted by Wordit Ltd <wo...@googlemail.com>.
On Thu, Nov 11, 2010 at 11:45 PM, Zachary Zolton
<za...@gmail.com> wrote:
>
>Yes, by all means, use their username in the doc ID. Then you
> wont need to do a query just to determine the ID.

Thanks, got it working. This is good stuff.

> If you're considering having other types of docs in the same DB,
> however, you'll probably wanna namespace the user doc IDs. For
> example, user "foobar" has a doc such as "user:foobar".

Hmm, I don't follow. I have a doc for user foobar with "_id:foobar".
If I had a second type of document, say one type with user's blog
posts and another with comments by same user, then do you mean I'd
need to add a field unique to usernames because the ID has already
been used?

Isn't the sticking point with update handlers that I have to know the
ID, so I could only use this method for one type of document, namely
those where doc ID is username?

Thanks,

Marcus

Re: Update handler doc ids

Posted by Zachary Zolton <za...@gmail.com>.
I didn't realize that the docs you were trying to update represent a
user! Yes, by all means, use their username in the doc ID. Then you
wont need to do a query just to determine the ID.

If you're considering having other types of docs in the same DB,
however, you'll probably wanna namespace the user doc IDs. For
example, user "foobar" has a doc such as "user:foobar".

—Zach

On Thu, Nov 11, 2010 at 2:00 PM, Wordit Ltd <wo...@googlemail.com> wrote:
> Zach,
>
> Thanks for the confirmation on the doc ID. I need to update a doc
> belonging to the logged in user. If I use a field like "owner":
> "username" then I would need to find the doc ID first. Don't you lose
> the advantage of an update handler by first doing a read request?
>
> I mean you might as well do a standard read then a write. Two calls.
> Update handlers have the advantage of a single db call, or not?
>
> To use an update handler, I need the ID. I figured that if I set the
> doc ID to be the username, which is unique, then I can send the
> username as the doc ID to the update handler. Is there anything wrong
> with that approach?
>
> Marcus
>
>
>
> On Thu, Nov 11, 2010 at 8:45 PM, Zachary Zolton
> <za...@gmail.com> wrote:
>>
>> Update handlers can't query the database nor access external
>> resources. They just update or create a single document based on the
>> HTTP request (and the current document object, in the case of updating
>> an existing document).
>>
>> Moreover, if you want to update a document, you'll need to provide the
>> ID. So, you can either construct the ID from variables available at
>> that point in your program, or make a query to find the doc ID before
>> calling your update handler.
>>
>> Give this a read:
>> http://wiki.apache.org/couchdb/Document_Update_Handlers
>>
>> If you provide more background about your scenario, I can give you
>> more explicit advice.
>>
>>
>> Cheers,
>>
>> Zach
>>
>>
>> On Thu, Nov 11, 2010 at 8:59 AM, Wordit Ltd <wo...@googlemail.com> wrote:
>>> Do update handlers require the doc id, or is it possible to search for
>>> the doc as with views?
>>>
>>> I need to update the document belonging to the logged in user. (I'll
>>> use a validation function to check that.)
>>> The only workaround I could think of is using the username as the doc
>>> id. Then I call the update handler with .../handler/username?param=x
>>>
>>> Marcus
>>>
>>
>

Re: Update handler doc ids

Posted by Wordit Ltd <wo...@googlemail.com>.
Zach,

Thanks for the confirmation on the doc ID. I need to update a doc
belonging to the logged in user. If I use a field like "owner":
"username" then I would need to find the doc ID first. Don't you lose
the advantage of an update handler by first doing a read request?

I mean you might as well do a standard read then a write. Two calls.
Update handlers have the advantage of a single db call, or not?

To use an update handler, I need the ID. I figured that if I set the
doc ID to be the username, which is unique, then I can send the
username as the doc ID to the update handler. Is there anything wrong
with that approach?

Marcus



On Thu, Nov 11, 2010 at 8:45 PM, Zachary Zolton
<za...@gmail.com> wrote:
>
> Update handlers can't query the database nor access external
> resources. They just update or create a single document based on the
> HTTP request (and the current document object, in the case of updating
> an existing document).
>
> Moreover, if you want to update a document, you'll need to provide the
> ID. So, you can either construct the ID from variables available at
> that point in your program, or make a query to find the doc ID before
> calling your update handler.
>
> Give this a read:
> http://wiki.apache.org/couchdb/Document_Update_Handlers
>
> If you provide more background about your scenario, I can give you
> more explicit advice.
>
>
> Cheers,
>
> Zach
>
>
> On Thu, Nov 11, 2010 at 8:59 AM, Wordit Ltd <wo...@googlemail.com> wrote:
>> Do update handlers require the doc id, or is it possible to search for
>> the doc as with views?
>>
>> I need to update the document belonging to the logged in user. (I'll
>> use a validation function to check that.)
>> The only workaround I could think of is using the username as the doc
>> id. Then I call the update handler with .../handler/username?param=x
>>
>> Marcus
>>
>

Re: Update handler doc ids

Posted by Zachary Zolton <za...@gmail.com>.
Marcus,

Update handlers can't query the database nor access external
resources. They just update or create a single document based on the
HTTP request (and the current document object, in the case of updating
an existing document).

Moreover, if you want to update a document, you'll need to provide the
ID. So, you can either construct the ID from variables available at
that point in your program, or make a query to find the doc ID before
calling your update handler.

Give this a read:
http://wiki.apache.org/couchdb/Document_Update_Handlers

If you provide more background about your scenario, I can give you
more explicit advice.


Cheers,

Zach


On Thu, Nov 11, 2010 at 8:59 AM, Wordit Ltd <wo...@googlemail.com> wrote:
> Do update handlers require the doc id, or is it possible to search for
> the doc as with views?
>
> I need to update the document belonging to the logged in user. (I'll
> use a validation function to check that.)
> The only workaround I could think of is using the username as the doc
> id. Then I call the update handler with .../handler/username?param=x
>
> Marcus
>