Beyond Frustrated Android SDK Couchbase Lite Querying

I have just about reached my wits end w.r.t using couchbase for a simple store/query operation. I have failed to find any documentation which can give me insight into my issue. To begin (using example code from the documentation) I attempt to store a document inside my database

    MutableDocument mutableDoc = new MutableDocument()
         .setFloat("version", 2.0F) 
         .setString("type", "SDK");

    // Save it to the database.
    try{
        database.save(mutableDoc);
    }
    catch (CouchbaseLiteException e){

    }

Once done, I attempt to retrieve said documents from the database using the following:

    // Create a query to fetch documents of type SDK.
    Query query = QueryBuilder.select(SelectResult.all())
            .from(DataSource.database(database))
            .where(Expression.property("type").equalTo(Expression.string("SDK")));
    try{
        ResultSet results = query.execute();

        for (Result result : results) {
            Log.i("SCRABBLER","In loop!");
            Log.i("SCRABBLER", "Type: " + (result.getValue("type"));
        }
    }
    catch (CouchbaseLiteException e){
        Log.e("SCRABBLER", e.getLocalizedMessage());
    }

Keep in mind, attempting to get the count correctly returns the number of documents and if I use the cblite tool I can see my documents in the database. What in god’s name am I missing here? It seems this should be a simple operation.

Calling getKeys() on the result returns one key with the name of the actual DB. Am I not using this SDK properly?

You are using the SDK correctly and the results you are seeing is right.

If you are querying for all the properties of the document using SelectResult.all() , then each member of the response array is a Key-Value pair, where the database name is the key and the dictionary corresponding to the Document is the value for the key.
The docs describe response format.

1 Like

Hi Priya,

If all I’m trying to achieve is to get all documents from the database and then iterate them and print out the documents, how do I go about it?

You are already getting the documents via the query. You just need to process the response the right way As I mentioned, the value corresponding to the database name key is the dictionary corresponding to the document). It is described in the same linked document
For example, this is code snippet for the documentation

for (Result result : rs) {
    Dictionary all = result.getDictionary(DATABASE_NAME);
    Log.i("Sample", String.format("name -> %s", all.getString("name")));
    Log.i("Sample", String.format("type -> %s", all.getString("type")));
}