Best practices on writing views with CB lite REST APIs


#1

Hi,

I’m having a hard time finding solid information on writing views with CB lite. I know there’s documentation for native, but there’s very little for Cordova. Instead, I’ve been using the documentation from CB Server, even though I know that CB lite only has subset of the Server view features.

I need to write a view where I’m joining two documents, like a blog post document and comment documents, so the output is a list of post documents with an array of comments embedded.

Any suggestions?


#2

I think you can not query info cross documents by View.


#3

Use the native documentation, and just mentally translate it from Java (or Obj-C or C#) to JavaScript. You can also make use of info on views in CouchDB.

You can’t directly do joins the way you would in a relational database, but you can use a combination of compound keys and reducing to do some of the same work. I gave an example of this in a Couchbase Connect presentation from 2014.


#4

Thanks @jens. I see the pseudo-join example on slide 21. I’ll get back to you on how it goes on my end.

BTW - Is groupLevel available in REST design documents?


#5

Yes, but it’s not part of the design doc, it’s part of the query. See the REST API docs for view queries.


#6

@jens,

I didn’t see any info for groupLevel. Shouldn’t it be on this page, http://developer.couchbase.com/documentation/mobile/1.2/develop/references/couchbase-lite/rest-api/design-document/get—db–design–design-doc–view–view-name-/index.html?


#7

You’re right; there are several parameters missing from that page. I’ve filed an internal bug report. Until then you can use the CouchDB docs for reference.


#8

@jens,

The page that describes creating a design doc mentions the “by_location” view that has a reduce function. But the code example is missing that view. Someone add that to the documentation?