Android-Cannot get custom ID document from database in Couchbase Lite

96 views
Skip to first unread message

Duy Bui

unread,
Jan 21, 2016, 11:59:27 AM1/21/16
to Couchbase Mobile
Hi everyone,
I'm new in Couchbase in Android. I faced a problem with document-id when save and get a list of document in database. I saved data into document with my id, here is my code to do that:

        Document document=cbDatabase.getDocument("my_id");
        Map<String,Object> docContent= new HashMap<String, Object>();
        docContent
.put("title", node.getTitle());
        docContent
.put("firstname", node.getFirstName());
        docContent
.put("lastname", node.getLastName());

       
try{
            document
.putProperties(docContent);
       
} catch (CouchbaseLiteException e){
           
Log.e(TAG, "Cannot write document to database", e);
       
}

But when I get that document, the return of id is not my custom ID, it's a default id (created by UUID). Here is the code to get id:
Query allDocumentsQuery= cbDatabase.createAllDocumentsQuery();
QueryEnumerator queryResult=allDocumentsQuery.run();
for (Iterator<QueryRow> it=queryResult;it.hasNext();){
   
QueryRow row=it.next();

   
String id=row.getDocumentId(); // I get id here and the result is an UUID (the default id)
}

Please give some advise and answer.
And I have another question too. I have a list of POJO object and I want to insert each of object to document with custom ID is index of that list. And here is my question: Why when I query this database, the results of response documents are not order like list. Thanks so much everyone!

Hideki Itakura

unread,
Jan 21, 2016, 1:35:35 PM1/21/16
to Couchbase Mobile
Hi Duy,

Your code is looks fine. Are you using CBL Andriod v1.1.0? We will try to reproduce the problem internally at couchbase.

> the results of response documents are not order like list. Thanks so much everyone!

Query result is ordered by key.

Thanks
Hideki

Duy Bui

unread,
Jan 21, 2016, 10:08:56 PM1/21/16
to Couchbase Mobile
I'm using CBL v1.1.0

Hideki Itakura

unread,
Jan 22, 2016, 2:55:30 AM1/22/16
to Couchbase Mobile
Hi Duy,

I wrote simple test code to reproduce the problem. However I can not reproduce the problem.

```
public void createDocs(View view) {
try {
for(int i = 0; i < 100; i++) {
Document doc = database.getDocument("doc-" + String.format("%05d", i));
Map<String, Object> props = new HashMap<>();
props.put("key", i);
doc.putProperties(props);
}
}catch (CouchbaseLiteException e){
Log.e(TAG, "Error to create new document", e);
}
}

public void printDocs(View view) {
try {
Query query = database.createAllDocumentsQuery();
QueryEnumerator e = query.run();
for(QueryRow row : e){
Log.e(TAG, "docID=" + row.getDocumentId());
}
}catch (CouchbaseLiteException e){
Log.e(TAG, "Error to iterate all documents", e);
}
}
```

Results:
```
01-21 23:48:53.054 13479-13479/? E/ThreadTest: docID=doc-00000
01-21 23:48:53.054 13479-13479/? E/ThreadTest: docID=doc-00001
01-21 23:48:53.054 13479-13479/? E/ThreadTest: docID=doc-00002
01-21 23:48:53.054 13479-13479/? E/ThreadTest: docID=doc-00003
01-21 23:48:53.055 13479-13479/? E/ThreadTest: docID=doc-00004
01-21 23:48:53.055 13479-13479/? E/ThreadTest: docID=doc-00005
01-21 23:48:53.055 13479-13479/? E/ThreadTest: docID=doc-00006
01-21 23:48:53.055 13479-13479/? E/ThreadTest: docID=doc-00007
...
...
01-21 23:48:53.081 13479-13479/? E/ThreadTest: docID=doc-00098
01-21 23:48:53.082 13479-13479/? E/ThreadTest: docID=doc-00099
```

Thanks,
Hideki

On Thursday, January 21, 2016 at 8:59:27 AM UTC-8, Duy Bui wrote:

Duy Bui

unread,
Jan 22, 2016, 5:02:55 AM1/22/16
to Couchbase Mobile
Thanks so much Hideki.
After take a few hours to read some documents and debug, I clear data on Android device and run again, the problem is solved. Thank you again for your example code.
Reply all
Reply to author
Forward
0 new messages