How do I load a random document from CouchDB (efficiently and fairly)?

前端 未结 5 727
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-03 10:46

I would like to load a random document out of a set of documents stored in a CouchDB database. The method for picking and loading the document should conform to the following re

5条回答
  •  别那么骄傲
    2021-02-03 11:35

    How about "abusing" a view's reduce function?

    function (keys, values, reduce) {
        if (reduce)
          return values[Math.floor(Math.random()*values.length)];
        else
          return values;
    }
    

提交回复
热议问题