You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by Apache Wiki <wi...@apache.org> on 2011/08/17 20:16:22 UTC

[Couchdb Wiki] Update of "Introduction_to_CouchDB_views" by BenjaminYoung

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.

The "Introduction_to_CouchDB_views" page has been changed by BenjaminYoung:
http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views?action=diff&rev1=38&rev2=39

Comment:
added doc._id to basic map emits as emit(null, doc) generates pretty pointless results; cleaned up some other phrasing

  
  {{{#!highlight javascript
  function(doc) {
-   emit(null, doc);
+   emit(doc._id, doc);
  }
  }}}
- This function defines a table that contains all the documents in a CouchDB database, with no particular key.
+ This function defines a table that contains all the documents in a CouchDB database, with the _id as the .
  
- A view function should accept a single argument: the document object. To produce results, it should call the implicitly available ''emit(key, value)'' function. For every invocation of that function, a result row is added to the view (if neither the ''key'' nor the ''value'' are undefined). As documents are added, edited and deleted, the rows in the computed table are updated automatically.
+ A view function should accept a single argument: the document object. To produce results, it should call the implicitly available ''emit(key, value)'' function. For every invocation of that function, a result row is added to the view (if neither the ''key'' nor the ''value'' are undefined). The rows in the computed table are updated automatically with any changes that have been made (additions, edits, or deletions) since the view w
  
  Here is a slightly more complex example of a function that defines a view on values computed from customer documents:
  
  {{{#!highlight javascript
  function(doc) {
    if (doc.Type == "customer") {
-     emit(null, {LastName: doc.LastName, FirstName: doc.FirstName, Address: doc.Address});
+     emit(doc._id, {LastName: doc.LastName, FirstName: doc.FirstName, Address: doc.Address});
    }
  }
  }}}
- For each document in the database that has a Type field with the value ''customer'', a row is created in the view. The ''value'' column of the view contains the ''!LastName'', ''!FirstName'', and ''Address'' fields for each document. The key for all the documents is null in this case.
+ For each document in the database that has a Type field with the value ''customer'', a row is created in the view. The ''value'' column of the view contains the ''!LastName'', ''!FirstName'', and ''Address'' fields for each document. The key for all the documents is still being set to the _id in this case.
  
  To be able to filter or sort the view by some document property, you would use that property for the key. For example, the following view would allow you to lookup customer documents by the ''!LastName'' or ''!FirstName'' fields: