How to transform SQL Where statements in couchbase lite

43 views
Skip to first unread message

Katharina Wehrle

unread,
Dec 22, 2014, 11:47:58 AM12/22/14
to mobile-c...@googlegroups.com

Hey Guys,
I build
my first Android App with Couchbase lite, now
I don't know how I can Query Data like a Where Clause in SQL. The structure of my json files are like that.

{
"id": 4001686390870,
"type": "foodstuffs",
"name": "Haribo Phantasia",
"tags": [
"Gummibears",
"sweets"
],
"ingredience": [
1,
2,
]}

Im SQL I just would want to know :

select type from table where type = 'foodstuffs'

How can I do this in my View ? I tried something in my code that looks like this

private String startQueryProduct() throws CouchbaseLiteException{

    com
.couchbase.lite.View viewItem = database.getView(String.format(
           
"%s/%s", designDocName, "QueryProduct"));
           
if (viewItem.getMap() == null) {
        viewItem
.setMap(new Mapper() {
           
@Override
           
public void map(Map<String, Object> document, Emitter emitter) {
               
Object docName  = document.get("name");
               
Object docType  = document.get("type");

               
if (docType != null && docType.equals("foodstuffs" ) {
                    emitter
.emit(docType.toString(),null);

               
}
           
}
       
}, "1.0");
   
}


   
Query query = viewItem.createQuery();
   
List<Object> l = new ArrayList<Object>();
    l
.add("foodstuffs");
    query
.setKeys(l);
     
String productName = "" ;
   
QueryEnumerator rowEnum = query.run();
   
for (Iterator<QueryRow> it = rowEnum; it.hasNext();) {
       
QueryRow row = it.next();

          productName
+= row.getDocument().getProperty("id")+"___"+row.getDocument().getProperty("type")  +"___"+row.getDocument().getProperty("name")  + "_____"+ "\n" ;


       
}
   
return productName ;

}


But in my App it shows me all my Items that are in my Database and doesn't select just the json files with type "foodstuffs".

On my Couchbase Server I made a View in my Shadow Bucket (I'm using Sync Gateway) named QueryProduct that looks like that

function (doc, meta) {
if ( doc.type == "foodstuffs")
emit
(doc.name, doc.id);
}


on my Server everything works fine. Can't I just copy this in my Android Code to use this ?

Jens Alfke

unread,
Dec 22, 2014, 12:25:43 PM12/22/14
to mobile-c...@googlegroups.com

On Dec 22, 2014, at 8:47 AM, Katharina Wehrle <kadai...@gmail.com> wrote:

                    emitter.emit(docType.toString(),null);

That should be docName, not docType, assuming you want this to work like the JavaScript view, i.e. it indexes only the documents whose type is 'foodstuffs'.

    List<Object> l = new ArrayList<Object>(); 
    l
.add("foodstuffs");
    query
.setKeys(l);

This is wrong. The view is already only indexing foodstuffs, and the key is the document name, not the type. You can just remove these lines.

          productName += row.getDocument().getProperty("id")+"___"+row.getDocument().getProperty("type") +"___"+row.getDocument().getProperty("name")  + "_____"+ "\n" ;

FYI, it's more efficient to use the row's key and value properties instead of the document (because fetching the document involves another database lookup.) Generally you should figure out what document attributes you want to use in the query, and have the map function emit those as the value.

—Jens

Jens Alfke

unread,
Dec 22, 2014, 12:26:51 PM12/22/14
to mobile-c...@googlegroups.com

> On Dec 22, 2014, at 8:47 AM, Katharina Wehrle <kadai...@gmail.com> wrote:
>
> on my Server everything works fine. Can't I just copy this in my Android Code to use this ?

No, because that's JavaScript code, not Java. Couchbase Lite doesn't have a JavaScript interpreter.

—Jens
Reply all
Reply to author
Forward
0 new messages