为了从其他人中过滤用户自己的文档,我将文档的创建者作为键或键的一部分返回:
org.couchdb.user:user1
或者
[org.couchdb.user:user1, otherkey]
这是创建“我的文档”页面的最佳方式吗?或者我可以简单地返回另一个键
otherkey
并用于userCtx
稍后过滤它?
为了从其他人中过滤用户自己的文档,我将文档的创建者作为键或键的一部分返回:
org.couchdb.user:user1
或者
[org.couchdb.user:user1, otherkey]
这是创建“我的文档”页面的最佳方式吗?或者我可以简单地返回另一个键
otherkey
并用于userCtx
稍后过滤它?
The answer depends on your architecture.
If you have some sort of "middleware" between your clients and the database, you can limit your data fetch from the second view pattern you describe ([org.couchdb.user:user1, otherkey]
) by querying ?startkey=["org.couchdb.user:user1"]&endkey=["org.couchdb.user:user1",{}]
. This limits the results to those between the first possible emitted key and the last, because a shorter array sorts before longer and an object sorts after other value types.
If you're trying to do as much validation and data display in CouchDB as possible (e.g. you're writing a "CouchApp") then what you should consider using filtered replication. This can be used to give each user their own personal database, e.g. a subset of a non-public master database containing only docs they should see. Then you can just emit plain document keys and assume your results will only include relevant documents.
There's a bit more background on the uses of filtered replication halfway down this blog post, and you may find more discussion on specific questions here as well.