I have a simple view with mapper that emits docs with some keys.
com.couchbase.lite.View view = database.getView(VIEW_NAME);
if (view.getMap() == null) {
Mapper map = new Mapper() {
@Override
public void map(Map<String, Object> document, Emitter emitter) {
if ("user".equals(document.get("type"))) {
emitter.emit(document.get("name"), document);
}
}
};
view.setMap(map, null);
}
having this View, i can than create Queries on it, with certain parameters like setKeys, startKey, endKey, setDescending, setDescending, setSkip and others as described in couchbase manual.
If i write
Query query = view.createQuery();
List<Object> keys = new ArrayList<>();
keys.add("User Name");
query.setKeys(keys);
that query would return all docs matching "User Name" key.
But I couldn't find a simple way to write queries that excludes (omits) docs with certain keys (like opposite to setKeys() function)
one hack was found in ToDoLite Example The code looks like this:
public static Query getQuery(Database database, final String ignoreUserId) {
com.couchbase.lite.View view = database.getView(VIEW_NAME);
if (view.getMap() == null) {
Mapper map = new Mapper() {
@Override
public void map(Map<String, Object> document, Emitter emitter) {
if ("user".equals(document.get("type"))) {
if (ignoreUserId == null ||
(ignoreUserId != null &&
!ignoreUserId.equals(document.get("user_id")))) {
emitter.emit(document.get("name"), document);
}
}
}
};
view.setMap(map, null);
}
Query query = view.createQuery();
return query;
}
Note that the view will only exclude key ignoreUserId you passed to it during the first call, and will ignore all the others during next calls (because it will create view only once during the first call)
So you need to create new view for every key they want to omit. But if you have a lot of keys you want to exclude or does it often, it would be inefficient and boilerplate.
Do you know any better solution or hack ?
Any help appreciated
Thanks in advance