Based on ToDoLite Android sample app I am trying to get query by ID working in my app.
Note that document is created with the same id as the content:
Document document = database.getDocument(userId);
If an existing key is set in the query method query.setKeys(keys);
the resulting enumerator is empty. If no keys are set to the query the enumerator contains all the elements emitted.
The Android documentation for using keys in a Query is here - http://developer.couchbase.com/mobile/develop/references/couchbase-lite/couchbase-lite/query/query/index.html#object-keys--get-set-
public static Query getQueryById(Database database, String userId) {
com.couchbase.lite.View view = database.getView(BY_ID_VIEW_NAME);
if (view.getMap() == null) {
Mapper map = new Mapper() {
@Override
public void map(Map document, Emitter emitter) {
if (DOC_TYPE.equals(document.get("type"))) {
emitter.emit(document.get("user_id"), document);
}
}
};
view.setMap(map, null);
}
// TODO: Adding keys here will result in an empty enumerator
Query query = view.createQuery();
java.util.List<Object> keys = new ArrayList<Object>();
keys.add(userId);
query.setKeys(keys);
return query;
}</code>
Once I get through this working, the next step would be to successfully query a View by any key in a document.
I would appreciate anyones input on how to query a View by a key on Android. Thanks.