2016-01-21 55 views
0

我在Couchbase的Android是新。我使用Couchbase Lite v1.1.0将本地数据保存。但是我在遇到一些问题时会这样做。我用Google搜索,在Couchbase精简版阅读文件并找到计算器的所有职位,但我还是不明白,我面对的。不能在Couchbase精简版获得自定义文档ID - Android电子

这里是我的代码片段演示代码保存在文件自定义的ID数据库我的数据是指数

cbManager=new Manager(new AndroidContext(context), 
        Manager.DEFAULT_OPTIONS); 
cbDatabase=cbManager.getDatabase("my_db"); 
       ..... 

for(int i=0; i<10; i++){ 
    Document document=cbDatabase.getDocument(String.valueOf(i)); // This line I custom document with id i 
    Map<String,Object> docContent= new HashMap<String, Object>(); 
    docContent.put("title", title); 
    docContent.put("firstName", firstName); 
    docContent.put("lastName", lastName); 
    try{ 
     document.putProperties(docContent); 
    } catch (CouchbaseLiteException e){ 
     Log.e(TAG, "Cannot write document to database", e); 
    } 
} 

而且从Couchbase精简版让所有提交的数据:

Query allDocumentsQuery= cbDatabase.createAllDocumentsQuery(); 
QueryEnumerator queryResult=allDocumentsQuery.run(); 
for (Iterator<QueryRow> it=queryResult;it.hasNext();){ 
     QueryRow row=it.next(); 

     Document doc=row.getDocument(); 
     String id=doc.getId(); // I get the id in here but the result is the default id (UUID):(
} 

所以,我有两个问题:

  1. 当我查询LL文件从数据库(couchbase精简版),该文件将返回其默认的ID(UUID),它为什么不回我的自定义ID?

    意思是:将所有文档保存到自定义ID为1,2,3的数据库中,但是从数据库获得的所有文档的结果都有默认的ID:UUID,UUID ,. ..,UUID。

  2. 我不明白为什么我按顺序保存文档,但是所有文档的返回都没有按顺序? (因为这个原因让我自定义id文件)

请给我一些建议或指导我做最好的方式来做到这一点。非常感谢你。

+1

您能够检索您的自定义ID创建的文档?我的意思是,你要执行例如: Document document = database.getDocument(“custom_id”); – sweetiewill

+0

哎呀,我尝试,我不能。为什么? – bkit4u

+0

这意味着您的文档没有保存与您提供的ID。它与生成的uuid一起保存。 –

回答

0

您需要的_rev属性与ID作为值添加到您的地图。

下面是来自documentation的摘录:

putProperties(Map<String, Object> properties) 
Creates and saves a new Revision with the specified properties. To succeed the specified properties must include a '_rev' property whose value maches the current Revision's id. 

所以,你的代码应该是这样的:

Map<String,Object> docContent= new HashMap<String, Object>(); 
docContent.put("_rev", String.valueOf(i)); 
docContent.put("title", title); 
docContent.put("firstName", firstName); 
docContent.put("lastName", lastName); 
相关问题