mongo db 聚合随机化(随机播放)结果

M.Z*_*.Z. 5 shuffle mapreduce mongodb aggregation-framework

我正在浏览一堆 mongo 文档,但找不到洗牌或随机化结果内容的可能性

有没有 ?

Nei*_*unn 1

特别是对于聚合框架本身来说,实际上并没有任何本地方法,因为还没有可用的运算符来执行诸如生成随机数之类的操作。因此,无论您可能投射一个字段进行排序的任何匹配,都不会因为缺乏变化的种子值而“真正随机”。

更好的方法是在返回结果后将结果“洗牌”为数组。有多种“shuffle”实现,下面是 JavaScript 的一种实现:

function shuffle(array) {
   var currentIndex = array.length
    , temporaryValue
    , randomIndex
    ;

  while (0 !== currentIndex) {
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果您实际上正在谈论对大量结果进行混洗,例如在使用 new$out运算符获得的集合中或实际上的任何集合中,那么您可以使用 mapReduce 来“作弊”。

db.collection.mapReduce(
    function(){
        var random = Math.floor( Math.random() * 100000 );
        emit({ rand: random, id: this._id }, this );
    },
    function(){},
    { out: { replace: "newcollection" } }
);
Run Code Online (Sandbox Code Playgroud)

这利用了 MapReduce 的特性,即键值始终是排序的。因此,通过包含随机数作为密钥的前导部分,您将始终获得随机排序的结果。