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 ?
On Dec 22, 2014, at 8:47 AM, Katharina Wehrle <kadai...@gmail.com> wrote:emitter.emit(docType.toString(),null);
List<Object> l = new ArrayList<Object>();
l.add("foodstuffs");
query.setKeys(l);
productName += row.getDocument().getProperty("id")+"___"+row.getDocument().getProperty("type") +"___"+row.getDocument().getProperty("name") + "_____"+ "\n" ;