Android: QueryRow.getDocumentId() returning null

query

#1

I am querying a simple view and getting null when asking for QueryRow.getDocumentId();

The view is defined as:

// Create View for Event List
        View expensesView = database.getView("expenseList");
        expensesView.setMapReduce(new Mapper() {
            @Override
            public void map(Map<String, Object> document, Emitter emitter) {
                if ("expense".equals(document.get("type"))) {
                    List<Object> key = new ArrayList<>();
                    key.add(document.get("event_id"));
                    key.add(document.get("created_at"));
                    emitter.emit(key, document.get("amount"));
                }
            }
        }, new Reducer() {
            @Override
            public Object reduce(List<Object> keys, List<Object> values, boolean rereduce) {
                float sum = 0;
                for (Object f : values) {
                    float val = ((Number) f).floatValue();
                    sum += val;
                }
                return sum;
            }
        }, "2");

Now, when I try to query this View and try to get the source document Id, I am getting null.

Query query = app.getDatabase().getView("expenseList").createQuery();
query.setMapOnly(true);
query.setDescending(true);
query.setGroupLevel(2);
query.setStartKey(Arrays.asList(eventId, new HashMap<String, Object>()));
query.setEndKey(Arrays.asList(eventId));
//Query for Results
QueryEnumerator result = query.run();
for (Iterator<QueryRow> it = result; it.hasNext(); ) {
    QueryRow row = it.next();
    String key = row.getKey().toString();
    String value = row.getValue().toString();
    Log.d("DatabaseHelper", "key:" + key + ", value:" + value + ", _id:" + row.getDocumentId() + "|" + row.getSourceDocumentId());
}

What I am getting output is:

key:[84ca9d7d-02d1-4c4a-bded-6f5b1a0cdf56, 1454924927419], value:400.0, _id:null|null
key:[84ca9d7d-02d1-4c4a-bded-6f5b1a0cdf56, 1454924308837], value:300.0, _id:null|null
key:[84ca9d7d-02d1-4c4a-bded-6f5b1a0cdf56, 1454924300656], value:200.0, _id:null|null
key:[84ca9d7d-02d1-4c4a-bded-6f5b1a0cdf56, 1454924283069], value:100.0, _id:null|null

How can any query return the document Id null ? It always has the source document id from which it was emitted.

Is there any point which I missed?


#2

You’re aggregating query rows by setting the groupLevel to 2, so they no longer correspond to individual documents.


#3

Ohh. I misunderstood it.

Can you please give me an example for groupLevel?. I misunderstood it with the startKey and endKey with array example.


#4

Here is the documentation for grouping.