Query on iOS failing with startkey, endkey

I have a map function in swift that emits an array of value in the format below:

[gggggg, yyyy, DDD, HH, mm, ss]

where g is a unique number that is padded with zeros, y is year, D is day of year (0-364), H is hour (0-23), m in minute, and s is second. All are strings and padded appropriately. A sample output is

[000001, 2015, 156, 05, 19, 20]
[000023, 2030, 004, 22, 40, 46]
[000234, 2200, 286, 04, 00, 00]

The idea is that I want to query as to get all documents for a given unique value that is sorted by date. However, despite reading countless articles and other forums, I have yet to get a query that returns a result using startkey and endkey. A query with exact keys does work.

I have tried
query.startKey = [“000000”,“0000”, “000”, “00”, “00”, “00”]
query.endKey = [“999999”, “9999”, “365”, “23”, “59”, “59”]

just to see if it works, but this still returns 0 results ( I have about 20 in my database).

query.startKey = [“000001”,“2000”, “000”, “00”, “00”, “00”]
query.endKey = [“000001”, “2300”, “365”, “23”, “59”, “59”]

Also returns nothing, despite having at least 10 documents that match the unique key “000001”

For reference, I am printing out my compound keys from the map function so that I know that my formatting is correct.

The parameters I have put on my query are
query.descending = true
query.mapOnly = true (have tried without specifying this as well)
query.inclusiveEnd = true/false/not included

Being that this is fed into a CBLUITableSource, I have tried running the query before and after I create the live query.

If I do not specify the start key or end key, all documents are returned from the map function.

Any help would be appreciated. I know this is a common topic but for some reason I can’t get it to work. Thank you for your time.

Are you certain that the keys you’re emitting contain strings? If you were emitting arrays of numbers, but querying for arrays of strings, it would cause exactly the effect you’re getting.

(It does seem strange that you’re using strings to represent numeric data. It would be somewhat more efficient to use numbers instead, and then you wouldn’t run into problems if, for example, you later need to expand gggggg to seven digits. Also, you could store the entire timestamp as a single value, either a string or a number (timeIntervalSinceReferenceDate).)

Oh! I just saw the problem. Since you’re using descending order, the iteration starts from larger keys, so the startKey has to be the larger value and the endKey the smaller value.

(This is a pretty common mistake…)

Works perfectly, thank you so much!

I was using strings just because that had the most documentation, I will probably shift to numbers now.