Hi, On 24 October 2011 08:28, Karl Seguin <[email protected]> wrote:
> Hi, > I'm just starting to learn CouchDB and I've accumulated four questions. > > First, I only found some vague references, but are views only updated on > read request? (assuming there's something to update). There's no built-in > mechanism to have views updated in the background say for every X > changed/new documents or X seconds? > Views by default are updated when queried, just before the read is performed. You can alter how a specific query affects the view, using the 'stale' parameter (see http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options). To update a view every N changes, perhaps you could use the 'stale=ok' for all queries but the N-th. To update a view every N seconds, you could use 'stale=ok' for all queries, while having some task running in the background that queries the view without 'stale' every N seconds. > > Secondly, it seems like if you want to update a document, you need to send > the complete document over. I understand that, given CouchDB's versioning, > this makes sense. In theory, would it be possible for CouchDB to expose an > API to allow updates to specific fields, then on the backend, it would > clone > the document and overwrite the field. Again, I know that isn't possible > with the current API, I'm just wondering if there's anything that would > stop > that from working. You'd essentially send over the doc_id, rev, the field > name and the new value. > Check out Update Handlers: http://wiki.apache.org/couchdb/Document_Update_Handlers > > Third, any bulk update or delete needs to be done in code by loop through > the result of a view? Say, I want to delete all the posts older than 1 > year. > I create view keyed by the post date, I query the view with my specific > filter, and then I loop through it deleting each document? It's pretty much > the same story for updates, but I can use the bulk update api. There's no > direct analog to delete from posts where date < ?, it's more of a select id > from posts where date < ? then delete those ids. Right? > No there isn't. You can also delete documents in bulk. See: http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API#Modify_Multiple_Documents_With_a_Single_Request > > Forth, and I'm sorry for asking this, I realize it's asked a lot, but I > couldnt' figure it out despite that...I'm trying to retrieve all of the > posts with a specific tag, sorted by date. My view looks like: > > function(doc) { > if (doc.doc_type != 'post') { return; } > for(var tag in doc.tags) { > emit([doc.dated, doc.tags[tag]], null); > } > } > > I was hoping that a query like this might work: > > for row in db.view('application/post_by_tags', key='[{}, "blah"]'): > print row > > But it doesn't. > > You're not looking for a single emitted row, so using the 'key' paramter is wrong here. Instead, you need to supply a range of keys - using the 'startkey' and 'endkey' parameters. You want all keys for a specific tag to be in the same key-range. So, first, you'd want all keys of emitted rows to be prefixed by the tag-name. Next, you want the emitted rows for each tag to have an internal order. That means that the second parameter should be the date: function(doc) { if (doc.doc_type != 'post') { return; } for(var tag in doc.tags) { emit([doc.tags[tag], doc.dated], doc); } } query it with "/?startkey=["blah"]&endkey=["blah", {}] > Thanks for the help, > Karl >
