Ordered query in swift with multiple keys

36 views
Skip to first unread message

Karel-Jan Van Haute

unread,
Oct 30, 2014, 12:16:47 PM10/30/14
to mobile-c...@googlegroups.com
Hello

I'm having the following code to get  all items from a parent. And I want to order them by index. So I get everything from key parentId, but then I want to get all the records ordered by index. 
To test it I added 0 and 6 witch are the current minimum and maximum in my list. 

So the question is how I can do that without giving the maximum. 


    func getList() -> CBLQuery{

        let view = self.database.viewNamed("list")

        let block: CBLMapBlock = { (doc: [NSObject: AnyObject]!, emit: CBLMapEmitBlock!) in

            if(doc["type"] as NSString == "item"){

                emit([doc["item_id"]!, doc["index"]! as Int], doc)

            }

        }

        view.setMapBlock(block, reduceBlock: nil, version: "1")

        var query = view.createQuery()

        var parentId = self.document.documentID

        query.startKey = [parentId, 0]

        query.endKey = [parentId, 6]

        return query

    }


Thank You

Jens Alfke

unread,
Oct 30, 2014, 1:56:47 PM10/30/14
to mobile-c...@googlegroups.com

On Oct 30, 2014, at 9:16 AM, Karel-Jan Van Haute <kare...@gmail.com> wrote:

To test it I added 0 and 6 witch are the current minimum and maximum in my list. 
So the question is how I can do that without giving the maximum. 

Use "an infinitely large value" instead of 6. The typical value people use for this kind of placeholder is an empty dictionary — @{} in Obj-C; I don't remember the syntax in Swift. (The object ordering used by the index will sort dictionaries after anything else, including strings/numbers/arrays.)

In CBL 1.0.3+ you can use the new CBLQuery.prefixMatchLevel property instead:
        query.startKey = [parentId]
        query.endKey = [parentId]
query.prefixMatchLevel = 1

—Jens
Reply all
Reply to author
Forward
0 new messages