Couchbase 2.0.0 simple query problem



I have been using Couchbase lite 1.4.1 without problems (other than performance issues) and saw the announcement of couchbase lite 2.0 for android, and since I deeply dislike views and prefer the new real-query approach Couchbase lite is taking I’m trying to migrate everything in my app from v1.4.1 to v2.0.0.

I use this code to save data:

    Map<String, Object> objectProperties= new HashMap<String, Object>(){
                        put("contact", new ObjectMapper().convertValue(getLead(), new TypeReference<Map<String, Object>>() {}));
                MutableDocument document = new MutableDocument(objectProperties);

I’m using jackson to convert my Object to Map since for some reason I cant use a POJO like it was in the 1.4.1 version.

And I’m using this code to retrieve (or attempt to) the saved data

Query query =
                ResultSet resultSet = query.execute();
                if(resultSet.allResults().size() == 0){
                    return ERROR_NO_DATA;
                for (Result result : resultSet) {
                    showInUI(ADD_ENTRY, result.getValue("contact"));

Here’s the problem, everytime I read the resultSet it has a size of zero. Am I doing something wrong?


Nothing looks obviously wrong, but I’d like to think this is too simple of a case for it to be a bug in the library. I imagine that the real answer is 3) Something else is going on besides what is shown here. Perhaps MyDatabaseController returning differing database instances, for example?


That is exactlyl what I thought, the use case is too simple for it to be a library problem, this is pretty much the example they have in the documentation.

I’ll continue trying. Do you think the replication could have an impact over this use case? Somehow messing with the Database data?


I think I found the problem. I didnt create an index and until I created one ( that my query returned results.


Value indices are optional and should not affect the results. @hideki Could you comment on this?


Can you try same code but first REMOVE the if check on size?
In other words comment out the following block. (Explanation follows.)

In my experience with the beta (have not looked at final version sources), checking allResults.size() causes an enumeration on the resultset, and unlike version 1.x where you can RESET a result set’s enumeration (index back to 0), with 2.x you cannot.
In other words getting the size (this way) from the resultset and using the resultset ‘normally’ is mutually exclusive.
Also, for testing, also select the document id (in the select clause) and log it to console in the resultset loop. Just to rule out any unrelated problems with document props and/or gui.
-nat (not a cb employee.)


Hi @jeav148,

@natgross Thank you for your comments.

For future enhancement and technical limitation, ResultSet only allows iteration. However, as many users need to know the size of results. So we introduced allResults() method. By calling allResults(), internal iterator reaches the end of the iterator.

If your application needs to know the size of ResultSet, please try following way:

List<Result> results = resultSet.allResults();
if(results.size() == 0){
   return ERROR_NO_DATA;
for (Result result : results) {
    showInUI(ADD_ENTRY, result.getValue("contact"));

I understand this is not obvious limitation. We will consider this for a future release.



What about sql COUNT() (and other aggregates), can I use [any of] those?



count(expr) should work as expected. But querying twice could be less efficient.


You are right. That is the problem. I was checking the size of the results and reaching the end of the iterator.

When I made it work I removed the zero-check and didnt think to much of it, I just tested to see if that is the problem and it is. Now is working as intended. Thank you all for your help.