0

In order to filter a user's own documents from others', I am returning the creator of the document as the key, or part of the key:

org.couchdb.user:user1

or

[org.couchdb.user:user1, otherkey]

Is this the best way to create a "My Documents" page? Or can I simply return another key

otherkey

and use userCtx to filter it later on?

Matt Norris
  • 8,596
  • 14
  • 59
  • 90

1 Answers1

0

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.

natevw
  • 16,807
  • 8
  • 66
  • 90