Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Summaried IRC conversation

  • Loading branch information...
commit f6aa114ef676cb7a1e3c885273b7234d1267a414 1 parent 99373a2
Ryan Kirkman authored March 30, 2011
50  _posts/2011-03-30-advanced-filtering-with-couchdb-views.textile
Source Rendered
@@ -11,40 +11,16 @@ Recently I had to sort a CouchDB view based on date while filtering that view by
11 11
 
12 12
 In CouchDB, I knew that sorting of view results is based upon the key. Therefore, I needed a way to filter by part of a complex key ( e.g. field1 and field 2 from [field1, field2, field3], which in my case would have been user and status from [user, status, date] ). But where do you go with a question like this if the internet doesn't want to play ball? IRC of course!
13 13
 
14  
-*Summary*: You have to use *startkey=* & *endkey=* if you want to filter by part of a complex key. If you want to filter using just *key=*, all parts of the complex key must be specified or you will get a null result, as *key=* is looking for an exact match.
15  
-
16  
-Note that when filtering by part of the complex key, you can only filter by in-order combinations. For example, if you had *[field1, field2, field3]* as a key, you could only filter by *[key1]*, *[key1, key2]* or *[key1, key2, key3]* (in their respective orders).
17  
-
18  
-<pre style="white-space: pre-wrap;">
19  
-<code>
20  
-<ryankirkman> How do I create a view that lets me filter by two arbitrary fields, and is ordered by date?
21  
-<ryankirkman> I am thinking now that I will have to use a startkey and endkey, with the date of the startkey set to some early date, and the date of the endkey set to some very distant date
22  
-<ryankirkman> And use a complex key like [field1, field2, date]
23  
-<okurow> ryankirkman: then you have to use field1 AND field 2
24  
-<okurow> and its sorted by 1.field1 2. field3 and 3rd date
25  
-<okurow> user [date,field1,field2] .. then you can use either field1, OR field1 & field2
26  
-<ryankirkman> okurow: That's cool
27  
-<okurow> and [date,field2,field1] then you can use date, either field2 OR field2 & field 1
28  
-<ryankirkman> I see
29  
-<ryankirkman> okurow: But is my assumption and the start and end key correct?
30  
-<ryankirkman> okurow: I feel like there has to be a nicer solution, but I can't think of it
31  
-<okurow> then startkey=[fromdate,key1]&endkey=[todate,key1,{}]
32  
-<ryankirkman> okurow: I can just omit values of the complex key?
33  
-<okurow> ryankirkman: ?? dont understand your question
34  
-<ryankirkman> I was under the impression that when filtering by complex key, if you had [date, field1, field2], you would have to specify all values
35  
-<ryankirkman> in your example above, you omitted key2 from the startkey
36  
-<okurow> ryankirkman: no
37  
-<okurow> just from left to right
38  
-<okurow> if you emit [date,key1,key2] you cannot seach for [date,key2] ...
39  
-<okurow> but you can use startkey=[date]&endkey=[date,{} to have all without key1,key2
40  
-<okurow> ( you will get key1,key2 in the list )
41  
-<ryankirkman> what does the null object at the end do?
42  
-<ryankirkman> I feel like it has higher order than anything your view may return, so you use it as a hack to get all results
43  
-<okurow> if you do not use it, the result will be null, cause, the range from start to end is zero ( endkeys are normally not included in the resultlist)
44  
-<ryankirkman> ahhh
45  
-<ryankirkman> It's interesting that I can't just do " key=[date] "
46  
-<okurow> so the endkey must be something that is higher than the expected last result
47  
-<okurow> you can.. but only if you emit (date)
48  
-<okurow> whenever you have a complex key.. you have to use startkey&endkey
49  
-</code>
50  
-</pre>
  14
+h2. Summary
  15
+
  16
+You have to use @startkey=@ & @endkey=@ if you want to filter by part of a complex key. If you want to filter using just @key=@, all parts of the complex key must be specified or you will get a null result, as @key=@ is looking for an exact match.
  17
+
  18
+Note that when filtering by part of the complex key, you can only filter by in-order combinations. For example, if you had @[field1, field2, field3]@ as a key, you could only filter by @[field1]@, @[field1, field2]@ or @[field1, field2, field3]@. You could not, for example, filter by @[field1, field3]@, as CouchDB would interpret the key you specified for @field3@ as the value to filter @field2@ by.
  19
+
  20
+The syntax required to use @startkey=...&endkey=...@ when you want to filter on only part of a complex key is as follows:
  21
+
  22
+Say we had a key like @[field1, field2, field3]@, and we wanted to filter on only @field1@ where @field1 = "123"@. Our url would look like:
  23
+
  24
+@http://localhost:5984/database/_design/design_doc/_view/view_name?startkey=["123"]&endkey=["123",{}]@
  25
+
  26
+Notice the @{}@ in the @endkey@. This is so that we get all values returned by the view between @null@ and @{}@, which for just about every case should be everything.

0 notes on commit f6aa114

Please sign in to comment.
Something went wrong with that request. Please try again.