Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Although Sam's answer is an ok pattern to follow I wanted to point out that there is, indeed, a nice way to append to a Couch document. <a href="http://blog.couch.io/post/410290711/atomic-increments-in-couchdb" rel="nofollow noreferrer">It just isn't very well documented yet.</a></p> <p>By defining an <code>update</code> function in your design document and using that to append to an array inside a couch document you may be able to save considerable disk space. Plus, you end up with a 1:1 correlation between the file you're logging accesses on and the couch doc that represents that file. This is how I imagine a doc might look: <code><pre> { "_id": "some/file/path/name.txt", "_rev": "32 char hash", "accesses": [ {"at": 1282839291, "by": "ben"}, {"at": 1282839305, "by": "kate"}, {"at": 1282839367, "by": "ozone"} ]<br> } </pre></code></p> <p><em>One caveat: You will need to encode the "/" as %2F when you request it from CouchDB or you'll get an error. Using slashes in document ids is totally ok.</em></p> <p>And here is a pair of map/reduce functions: <code><pre> function(doc) { if (doc.accesses) { for (i=0; i &lt; doc.accesses.length; i++) { event = doc.accesses[i]; emit([doc._id, event.by, event.at], 1); } } } </pre></code> <code><pre> function(keys, values, rereduce) { return sum(values); } </pre></code></p> <p>And now we can see another benefit of storing all accesses for a given file in one JSON document: to get a list of all accesses on a document just make a get request for the corresponding document. In this case:</p> <p><code>GET <a href="http://127.0.0.1:5984/dbname/some%2Ffile%2Fpath%2Fname.txt" rel="nofollow noreferrer">http://127.0.0.1:5984/dbname/some%2Ffile%2Fpath%2Fname.txt</a></code></p> <p>If you wanted to count the number of times each file was accessed by each user you'll query the view like so:</p> <p><code>GET <a href="http://127.0.0.1:5984/test/_design/touch/_view/log?group_level=2" rel="nofollow noreferrer">http://127.0.0.1:5984/test/_design/touch/_view/log?group_level=2</a></code></p> <p>Use group_level=1 if you just want to count total accesses per file.</p> <p>Finally, here is the <code>update</code> function you can use to append onto that doc.accesses array: <code><pre> function(doc, req) { var whom = req.query.by; var when = Math.round(new Date().getTime() / 1000);</p> <p>if (!doc.accesses) doc.accesses = []; var event = {"at": when, "by": whom} doc.accesses.push(event); var message = 'Logged ' + event.by + ' accessing ' + doc._id + ' at ' + event.at; return [doc, message]; } </pre></code></p> <p>Now whenever you need to log an access to a file issue a request like the following (depending on how you name your design document and update function):</p> <p><code><pre><a href="http://127.0.0.1:5984/my_database/_design/my_designdoc/_update/update_function_name/some%2Ffile%2Fpath%2Fname.txt?by=username" rel="nofollow noreferrer">http://127.0.0.1:5984/my_database/_design/my_designdoc/_update/update_function_name/some%2Ffile%2Fpath%2Fname.txt?by=username</a></pre></code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload