On 5 February 2011 23:59, Timothy Wood <[email protected]> wrote:
>
> I’ve been playing with the _changes support in concert with the filter
> parameter. I’m seeing some odd results, though.
>
> In all these cases, my filter function looks like:
>
> function(doc, req) {
> if (doc.type == "item" && doc["_attachments"]) {
> return true;
> }
> return false;
> }
>
>
> 1) If I query for changes with a filter like:
>
>
> http://status-board:5984/status-board/_changes?feed=continuous&filter=app/items
>
> I’ll see my test documents (that match the filter) come back. Great. But then
> if I go and delete a document using Futon, no “_deleted”:true change comes
> back. If I leave off the filter, I do get a _deleted entry in the changes
> feed.
Unfortunately, a deleted document does not retain existing attributes.
A deleted doc is basically just a stub containing _id, _rev and
_deleted attributes.
Personally, I think it would be very useful to retain a document's
existing attributes when deleted, i.e. add a _deleted attribute and
leave everything else alone.
>
> 2) If I edit the document in Futon (by deleting its last attachment) so that
> it no longer matches the filter, I don’t get a _deleted entry. I would expect
> to get one since the change feed reported the document to me (it matched the
> filter) and then it was edited to not match the filter. For an observer
> following a filtered feed, this delete seems pretty crucial (if you wanted to
> do a filtered replication, for example).
I don't really understand why you would expect a _deleted row in the
changes feed here. The document has not been deleted, just changed,
and your filter function explicitly says it's not interested in
documents with no attachments.
You should remove the test for doc["_attachments"] and handle
attachments vs no attachments in your change feed handled.
>
> If I start a an unfiltered _changes feed and then do an insert of a document
> followed by adding an attachment, I get two lines in the _changes feed. But,
> if I use the filter, I only get one. That is, unlike the delete case, the
> filter doesn’t note the document until the document matches the filter.
> Probably a happy accident, but the mirror case (edit to not match the filter)
> should be symmetric and issue a delete change.
It's really not a happy accident - it's exactly what your filter
function tells CouchDB to do.
>
> In my particular use case, I can probably just drop the filter on the server
> side and do it in the client. Is there something wrong in my use of _changes
> with a filter? I don’t see any discussion of this in the book or on the
> mailing list, but maybe I missed it.
You don't need to drop the filter func but you might have to
compromise a little. Change it to the following:
function(doc, req) {
return doc._deleted || doc.type == "item";
}
You can then:
* Handle attachments vs no attachments correctly
* Clean up after a document gets deleted.
Note that you will see *all* deleted documents and not just documents
that used to have an item: "type" attribute. Admittedly, that's not
ideal but, in my experience, not a massive problem either.
Hope that helps.
- Matt