No can do; that simply isn’t how indexes (in any database) work. Think of the index as a big on-disk array of {key, value} pairs that’s always sorted by the key. It’s very efficient — O(log n) time — to find an item with a given key, and stepping to the
next item takes a very small constant time. But iterating the entire index is slow, O(n). For more details, consult Wikipedia for “search tree” or “b-tree”.
Your view’s index is sorted by Form, then Record, then Field. So its ordering looks like
form1, record1, field1
form1, record1, field2
form1, record1, field3
form1, record2, field1
form1, record2, field2
form2, record1, field1
form3, record1, field1,
form4, record2, field2
…
where “1”, “2”, etc. are just placeholders showing the lexical order that the different values appear in.
As you can see from this, if you ask for all rows where the key contains “field2”, there’s no quick way to find them, because they’re scattered all through the index. So the database has to do a slow linear scan of the entire index, comparing the 3rd item
of each key with “field2”.
Instead, the way to find “field2” efficiently is to create another index, whose primary key is Field. Then all the “field2” values will be adjacent to each other and can quickly be found and scanned.
Again, this holds true for any type of database, it’s just that you’re probably used to SQL which will do more work for you — its approach is to perform any query you ask of it, however inefficient. So a SQL database would let you do what you’re asking;
you’d just find as you scaled up your database that the query got really slow. And then you might ask a SQL guru or do some research, and find that the answer is to run another CREATE INDEX statement to make an index with Field as its primary key. Exactly
as with Couchbase Lite.
(Note that you could use the CBLQueryBuilder, which is sort of SQL-like in that it can look at your high-level predicates and figure out how to convert them into an index-based query. It acts differently than SQL in that it will automatically create a
view and index as necessary to make the query fast. Which is better than SQL in some ways, but has the drawback that you might end up with too many views/indexes, which will slow down querying after an insertion.)
Hope this helps, and hope I didn’t come off as patronizing :)
—Jens