2013-10-25 30 views
0

我正在開發基於用戶選擇日期顯示事件的Android應用程序。我正在使用Couchdb和Ektorp。我不知道如何定義方法findByMes的視圖,其中包含一個變量日期,我應該從日曆中讀取而不是「2013-10-01」... 這是de class EventDB的代碼工作。它可能放在@View動態參數?

如果有人能幫助我,我將非常感激!

public class EventDAO extends CouchDbRepositorySupport<EventVO> { 

     public EventDAO(CouchDbConnector db) { 
       super(EventVO.class, db); 
       initStandardDesignDocument(); 
     } 

     @GenerateView @Override 
     public List<EventVO> getAll() { 
       ViewQuery q = createQuery("all") 
           .includeDocs(true); 
       return db.queryView(q, EventVO.class); 
     } 

     public List<EventVO> findByDate(String Date) { 
      List<EventVO> event = queryView("by_date", Date); 
      return event; 
     } 

     @View(name = "by_mes", map ="function(doc) {if (doc.type == 'Event' && doc.date >= '2013-10-01' && doc.date <= '2013-10-31' ) { emit(doc._id,doc)}}") 
     public List<EventVO> findBymes() { 
      ViewQuery q = createQuery("by_mes") 
        .includeDocs(true); 
      return db.queryView(q, EventVO.class); 

     } 
} 

回答

0

由於視圖只構建一次,並且增量更新僅適用於已更改的文檔,因此在視圖內不可能具有動態內容。你應該日期位從地圖功能移動到關鍵:

function(doc) { 
    if (doc.type == 'Event'){ 
     var key = doc.date.split('-').map(function(i){ 
      return parseInt(i, 10); 
     }); // .map part is optional, I prefer to have date-time arrays with int values 
     key.push(doc._id); 
     emit(key, doc); 
    } 
} 

,並在查詢參數使用它們(日期位):

http://localhost:5984/db/_design/ddoc/_view/by_mes?startkey=[2013,10,1]&endkey=[2013,10,31, {}] 

這會給你一個不同的輸出(I省略value的doc對象):

{ 
    "offset": 0, 
    "rows": [ 
     { 
      "id": "foo", 
      "key": [2010, 10, 1, "foo"], 
      "value": {} 
     }, 
     { 
      "id": "bar", 
      "key": [2010, 10, 11, "bar"], 
      "value": {} 
     }, 
     { 
      "id": "baz", 
      "key": [2010, 10, 31, "baz"], 
      "value": {} 
     } 
    ], 
    "total_rows": 3 
} 

但能夠隨時更改所需的日期範圍。