CouchDB: Return Newest Documents of Type Based on Timestamp

前端 未结 3 436
清酒与你
清酒与你 2021-01-15 14:33

I have a system that accepts status updates from a variety of unique sources, and each status update creates a new document in the following structure:

{
 \"         


        
3条回答
  •  天涯浪人
    2021-01-15 15:23

    I suspect that it's slow only because you emit the entire document, which means a lot of data needs to be stored and moved around to compute your final values. Try emitting the timestamp instead:

    function(doc) {
      if (doc.type == "status_update") {
        emit(doc.source_id, [doc._id,doc.timestamp]);
      }
    }
    
    function(keys, values, rereduce) {
      var winner = values[0];
      var i = values.length;
      while (i--) {
        var val = values[i];
        if (val[1] > winner[1]) winner = val;
      }
      return winner;
    }
    

    This should get you an [id,timestamp] pair for every key without being too slow or having to store too much data in the views.

    Once you have a list of identifiers on the client, send a second request using the bulk GET API:

    _all_docs?keys=[id1,id2,id3,...,idn]&include_docs=true 
    

    This will grab all the documents in one request.

提交回复
热议问题