1

在我的 couchDB 数据库中,我有一个过滤器,它只发出doc.my_key == "value"这样的文档:

{
  "id": "_design/test".
  "filters": {
    "value_only": "function(doc){return (doc['my_key'] == 'value');}"
  }
}

当我写两个这样的文件时:

{
  "id": 1,
  "my_key": "cheeky"
}

{
  "id":2,
  "my_key": "value"
}

然后 json at/this_database/_changes?filter=test/value_only读取:

{
  "results":[
    {"seq":2,"id":"2","changes":[{"rev":"1-463e18b34dfa529dd9b39981ad3293f4"}]},
  ],
  "last_seq":4
}

这很酷。CouchDB 很酷。但是我现在像这样更新文档#2:

{
  "id":2,
  "my_key": "no longer value"
}

过滤后的更改提要现在为空。我知道这是因为当文档更新时,该文档的先前 seq 编号会从更改提要中删除,但是是否有任何参数我可以传递/我可以进行配置编辑,以便返回最新发布的版本证件的身份证?

4

1 回答 1

1

Suddenly, there is no such parameter for changes feed. The only way you may handle all changes events is to listen feed as continuous stream. However, even this case couldn't give you any warranty that received revision number will be available due to race condition with compaction operation.

The changes feed emits only leafs revisions and the winning ones if there was any conflicts(e.g. document has two "heads" with different leaf revision). For latter ones you may pass ?style=all_docs query argument to retrieve all existed leafs for document.

For example:

+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
' Document with _id foo                                             '
'                                                                   '
' +-----------------+       +-------+       +-------+     +-------+ '
' |      1-abc      | ----> | 2-cde | ----> | 3-fgh | --> | 4-ijk | '
' +-----------------+       +-------+       +-------+     +-------+ '
'                             |                                     '
+ - - - - - - - - - - - -     |         - - - - - - - - - - - - - - +
                          '   |       '
                          '   |       '
                          '   v       '
                          ' +-------+ '
                          ' | 3-123 | '
                          ' +-------+ '
                          '           '
                          + - - - - - +

The default behaviour (style=main_only) will yield the winning revision: 4-ijk, while with style=all_docs the changes feed yields both 3-123 and 4-ijk revisions for this document.

Not sure in, but I suppose that is optimized usage of database data structure and protection against conflicts with compaction which handles all non leafs revisions.

于 2013-01-15T11:01:58.763 回答