2011-04-21 48 views
0

我有一個觀點定義爲:查詢視圖不CouchDB中返回任何值

function(doc) 
{ 
    if (doc.type="user") 
    { 
     emit([doc.uid, doc.groupid], null); 
    } 
} 

在Java代碼中,我已經寫了

List<String> keys = new ArrayList<String>(); 
keys.add("93"); 
keys.add("23"); 
ViewQuery q = createQuery("getProfileInfo").descending(true).keys(keys).includeDocs(true); 
ViewResult vr = db.queryView(q); 
List<Row> rows = vr.getRows(); 
for (Row row : rows) { 
    System.out.println("Key--->"+row.getKey()); 
    System.out.println("Value--->"+key); 
} 

我的代碼總是返回0行 - 你有什麼我錯過了?

+0

在清理代碼的格式時,我注意到你的map函數正在做一個賦值(=)而不是比較(==)。這應該是你修復的第一件事。 – 2011-04-22 17:32:17

+0

對於有足夠權限這樣做的任何人,您能否爲[ektorp](http://code.google.com/p/ektorp/)添加一個標籤,上面代碼中使用的CouchDB Java庫? – 2011-04-22 17:35:47

+0

你可以發佈視圖中的原始JSON結果嗎? – 2011-04-22 20:32:00

回答

1

修改代碼以

ComplexKey keys = ComplexKey.of(「93」,」23」); 
ViewQuery q = createQuery("getProfileInfo").descending(true).key(keys).includeDocs(true); 
ViewResult vr = db.queryView(q); 
List<Row> rows = vr.getRows(); 
for (Row row : rows) { 
    System.out.println("Key--->"+row.getKey()); 
    System.out.println("Value--->"+row.getValue()); 
} 

,現在工作得很好。

1

我懷疑你有一個類型不匹配,但是不可能在沒有看到視圖行的情況下確認。所以,如果我錯了,請從視圖中發佈示例行。

'keys'在發送到CouchDB之前被編碼爲JSON。你添加了兩個字符串 - 「93」和「23」 - 但我猜他們實際上是文檔中的整數。在JSON中,字符串和整數的編碼方式不同。一對字符串編碼爲[「93」,「23」],一對整數編碼爲[93,23]。

如果我是正確的,那麼'keys'應該被定義爲List <Integer>(或者看起來在Java中)。

+0

當我從瀏覽器中這樣查詢時,我得到如下結果http:// localhost:5984/testdb/_design/TestUserDoc/_view/getProfileInfo?key = [「93」,「23」]結果:{「total_rows」: 483,「offset」:461,「rows」:[{「id」:「16」,「key」:[「93」,「23」],「value」:null}]}但是在Java中, 0行。我已將所有字段存儲爲couchdb中的字符串。 – Ajay 2011-04-26 08:43:12

+0

我將代碼更改爲以下內容,現在可以使用ComplexKey keys = ComplexKey.of(「93」,「23」); ViewQuery q = createQuery(「getProfileInfo」)。descending(true).key(keys).includeDocs(true); ViewResult vr = db.queryView(q);列表 rows = vr.getRows(); for(Row row:rows){System.out.println(「Key --->」+ row.getKey());的System.out.println( 「值--->」 + row.getValue()); } – Ajay 2011-04-26 10:35:59