Query syntax help

I’ve been reading and rereading the documentation and I just can’t wrap my head around the query syntax for getting specific data. I’m doing this in iOS.

I’ve got documents that look similar to this:

{
sessionId: string,
sequence: integer,
…other data
}

What I need is to grab documents (with all the data) where it matches a given sessionId AND sequence.

I created a view with map like this:


CBLView *viewEpochs = [manager.database viewNamed:kCBLQueryViewForFetchEpochsBySessionIdAndSequence];
if (!viewEpochs.mapBlock) {
[viewEpochs setMapBlock:^(NSDictionary *doc, CBLMapEmitBlock emit) {
NSString *type = NSStringFromClass([COPModelDataEpoch class]);
if ([doc[@“type”] isEqualToString:type]) {
NSArray *keys = @[doc[@“sessionId”], doc[@“sequence”]];
emit(keys, nil);
} version:@“1”];

Now how do I create the query and pass in sessionId and sequence?

CBLQuery* query = [viewEpochs createQuery];
query.startKey = query.endKey = @[sessionID, sequence];
for (CBLQueryRow* row in [query run: &error]) {
    CBLDocument* doc = row.document;
    ....
}

If there’s specific data you need out of the document, it’s more efficient to emit that as the value in the emit call, so you can read it directly from row.value instead of having to load the document into memory.

You may also want to try out the CBLQueryBuilder class, which exposes a higher-level API more like Core Data.

Thanks for the quick response.

Ok, so in your example the @[sessionID, sequence] would be the data I want to get (vs. key names), yes? example: @[“1234A”, 10]

I did look a little on CBLQueryBuilder class. One thing that I didn’t see any information about was with the select array and whether there was a shortcut for returning all (like * in SQL).

Yes, sessionID and sequence are the values that you want to find.

In any query you can always get the equivalent of “*” by using CBLQueryRow.document to read the entire document. It’s just more efficient to specify a subset of the keys up-front.