Views are ordered by key, you can’t do "ends with" queries without reading the
entire view and filtering at the client.
You *can* do "begins with" using startkey/endkey, since those are adjacent rows.
for example,
map:
function(doc) {
emit([doc.town, doc.city], null);
}
query with ?startkey=["Anytown"]&endkey=["Anytown",{}];
{} is *not* a wildcard, it’s just an empty JSON object, but objects sort higher
than strings, so this startkey/endkey combination captures any of the emitted
values where the first item was "Anytown".
The full details of what values sort higher than other values is here:
https://wiki.apache.org/couchdb/View_collation
B.
On 12 Feb 2014, at 22:35, Tito Ciuro <[email protected]> wrote:
> Hello,
>
> I have a database with documents with the following format:
>
> {
> "_id" : "12345",
> "location" : <some_city>.<some town>
> }
>
> I have values like:
>
> California.San Francisco
> California.Los Angeles
> Florida.Miami
> ...
> ...
>
> What I'm trying to do is to match documents that "end with" a particular
> string. Say I want to match all states where the town 'Anytown' exists:
>
> California.Anytown
> Florida.Anytown
> Texas.Anytown
>
> If I use the following query:
>
> curl
> http://127.0.0.1:5984/example/_design/test/_view/ends-with-city?key=%22California.Anytown%22
>
> It works and returns one document (as expected), but I cannot seem to make it
> work when I look for an "end with" string. I have seen examples where %007F
> is used to match "begins with". When I use it, I get a
> {"error":"bad_request","reason":"invalid_json"} message. So I'm not sure how
> to proceed.
>
> Any ideas? Thanks!
>
> -- Tito