couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Couchdb Wiki] Update of "ViewCollation" by NoahSlater
Date Sat, 29 Mar 2008 19:04:33 GMT
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 NoahSlater:
http://wiki.apache.org/couchdb/ViewCollation

The comment on the change is:
Copied from original CouchDB wiki

New page:
A simple introduction to CouchDB view collation.

== Basics ==

View functions specify a key and a value to be returned for each row. CouchDB collates the
view rows by this key. In the following example, the !LastName property serves as the key,
thus the result will be sorted by !LastName:

{{{
function(doc) {
  if (doc.Type == "customer") {
    map(doc.LastName, {FirstName: doc.FirstName, Address: doc.Address});
  }
}
}}}

CouchDB allows arbitrary JSON structures to be used as keys. You can use complex keys for
fine-grained control over sorting and grouping.

== Examples ==

The following clever trick would return both customer and order documents. The key is composed
of a customer ''_id'' and a sorting token. Because the key for order documents begins with
the ''_id'' of a customer document, all the orders will be sorted by customer. Because the
sorting token for customers is lower than the token for orders, the customer document will
come before the associated orders. The values ''0'' and ''1'' for the sorting token are arbitrary.

{{{
function(doc) {
  if (doc.Type == "customer") {
    map([doc._id, 0], doc);
  } else if (doc.Type == "order") {
    map([doc.customer_id, 1], doc);
  }
}
}}}

This trick was [http://www.cmlenz.net/blog/2007/10/couchdb-joins.html originally documented]
by Christopher Lenz.

== Collation Specification ==

This section is based on the ''view_collation'' function in ''couch_tests.js'':

{{{
// special values sort before all other types
null
false
true

// then numbers
1
2
3.0
4

// then text, case sensitive
"a"
"A"
"aa"
"b"
"B"
"ba"
"bb"

// then arrays. compared element by element until different.
// Longer arrays sort after their prefixes
["a"]
["b"]
["b","c"]
["b","c", "a"]
["b","d"]
["b","d", "e"]

// then object, compares each key value in the list until different.
// larger objects sort after their subset objects.
{a:1}
{a:2}
{b:1}
{b:2}
{b:2, a:1} // Member order does matter for collation.
           // CouchDB preserves member order
           // but doesn't require that clients will.
           // this test might fail if used with a js engine
           // that doesn't preserve order
{b:2, c:2}
}}}

Mime
View raw message