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 2008/05/28 22:13:25 UTC
[Couchdb Wiki] Update of "Views" by ChrisAnderson
Dear Wiki user,
You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.
The following page has been changed by ChrisAnderson:
http://wiki.apache.org/couchdb/Views
The comment on the change is:
changed references to map() to emit()
------------------------------------------------------------------------------
Note that by default views are not created and updated when a document is saved, but rather, when it is accessed. As a result, the first access might take some time depending on the size of your data while CouchDB creates the view. If preferable the views can also be updated when a document is saved using an external script that calls the views when updates have been made. An example can be found here: RegeneratingViewsOnUpdate
+ Note on !JavaScript API change: Prior to Tue, 20 May 2008 (Subversion revision r658405) the function to emit a row to the map index, was named "map". It has now been changed to "emit".
+
== Basics ==
Here is the simplest example of a view function:
{{{
function(doc) {
- map(null, doc);
+ emit(null, doc);
}
}}}
This function defines a table that contains all the documents in a CouchDB database, with no particular key.
- A view function should accept a single argument: the document object. To produce results, it should call the implicitly available ''map(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). As documents are added, edited and deleted, the rows in the computed table are updated automatically.
Here is a slightly more complex example of a function that defines a view on values computed from customer documents:
{{{
function(doc) {
if (doc.Type == "customer") {
- map(null, {LastName: doc.LastName, FirstName: doc.FirstName, Address: doc.Address});
+ emit(null, {LastName: doc.LastName, FirstName: doc.FirstName, Address: doc.Address});
}
}
}}}
@@ -45, +47 @@
{{{
function(doc) {
if (doc.Type == "customer") {
- map(doc.LastName, {FirstName: doc.FirstName, Address: doc.Address});
+ emit(doc.LastName, {FirstName: doc.FirstName, Address: doc.Address});
- map(doc.FirstName, {LastName: doc.LastName, Address: doc.Address});
+ emit(doc.FirstName, {LastName: doc.LastName, Address: doc.Address});
}
}
}}}
@@ -87, +89 @@
== Lookup Views ==
- The second parameter of the ''map()'' function can be ''NULL''. CouchDB then only stores the key in the view. If you use the document's ID here, you can use the view as a compact lookup mechanism and fetch the document's details, if needed, in subsequent requests.
+ The second parameter of the ''emit()'' function can be ''NULL''. CouchDB then only stores the key in the view. If you use the document's ID here, you can use the view as a compact lookup mechanism and fetch the document's details, if needed, in subsequent requests.
== Complex Keys ==