来自Mongoose集合的随机文档

时间:2021-08-02 02:36:33

I want to create a Schema.statics.random function that gets me a random element from the collection. I know there is an example for the native MongoDB driver, but I can't get it working in Mongoose.

我想创建一个Schema.statics.random函数,它从集合中获取一个随机元素。我知道有一个本机MongoDB驱动程序的例子,但我不能让它在Mongoose中工作。

4 个解决方案

#1


26  

I found this Mongoose Schema static function in a GitHub Gist, which should achieve what you are after. It counts number of documents in the collection and then returns one document after skipping a random amount.

我在GitHub Gist中找到了这个Mongoose Schema静态函数,它应该实现你所追求的目标。它计算集合中的文档数量,然后在跳过随机数量后返回一个文档。

QuoteSchema.statics.random = function(callback) {
  this.count(function(err, count) {
    if (err) {
      return callback(err);
    }
    var rand = Math.floor(Math.random() * count);
    this.findOne().skip(rand).exec(callback);
  }.bind(this));
};

Source: https://gist.github.com/3453567

资料来源:https://gist.github.com/3453567

NB I modified the code a bit to make it more readable.

NB我稍微修改了一下代码以使其更具可读性。

#2


11  

If you are not wanting to add "test like" code into your schema, this uses Mongoose queries.

如果您不想在模式中添加“类似测试”代码,则使用Mongoose查询。

Model.count().exec(function(err, count){

  var random = Math.floor(Math.random() * count);

  Model.findOne().skip(random).exec(
    function (err, result) {

      // result is random 

  });

});

#3


6  

I've implemented a plugin for mongoose that does this in a very efficient way using a $near query on two randomly generated coordinates using a 2dsphere index. Check it out here: https://github.com/matomesc/mongoose-random.

我已经为mongoose实现了一个插件,它使用2dsphere索引在两个随机生成的坐标上使用$ near查询以非常有效的方式执行此操作。请在此处查看:https://github.com/matomesc/mongoose-random。

#4


0  

For people looking at this in times of async/await, promises etc.:

对于在异步/等待期间看到这个的人,承诺等:

MySchema.statics.random = async function() {
  const count = await this.count();
  const rand = Math.floor(Math.random() * count);
  const randomDoc = await this.findOne().skip(rand);
  return randomDoc;
};

#1


26  

I found this Mongoose Schema static function in a GitHub Gist, which should achieve what you are after. It counts number of documents in the collection and then returns one document after skipping a random amount.

我在GitHub Gist中找到了这个Mongoose Schema静态函数,它应该实现你所追求的目标。它计算集合中的文档数量,然后在跳过随机数量后返回一个文档。

QuoteSchema.statics.random = function(callback) {
  this.count(function(err, count) {
    if (err) {
      return callback(err);
    }
    var rand = Math.floor(Math.random() * count);
    this.findOne().skip(rand).exec(callback);
  }.bind(this));
};

Source: https://gist.github.com/3453567

资料来源:https://gist.github.com/3453567

NB I modified the code a bit to make it more readable.

NB我稍微修改了一下代码以使其更具可读性。

#2


11  

If you are not wanting to add "test like" code into your schema, this uses Mongoose queries.

如果您不想在模式中添加“类似测试”代码,则使用Mongoose查询。

Model.count().exec(function(err, count){

  var random = Math.floor(Math.random() * count);

  Model.findOne().skip(random).exec(
    function (err, result) {

      // result is random 

  });

});

#3


6  

I've implemented a plugin for mongoose that does this in a very efficient way using a $near query on two randomly generated coordinates using a 2dsphere index. Check it out here: https://github.com/matomesc/mongoose-random.

我已经为mongoose实现了一个插件,它使用2dsphere索引在两个随机生成的坐标上使用$ near查询以非常有效的方式执行此操作。请在此处查看:https://github.com/matomesc/mongoose-random。

#4


0  

For people looking at this in times of async/await, promises etc.:

对于在异步/等待期间看到这个的人,承诺等:

MySchema.statics.random = async function() {
  const count = await this.count();
  const rand = Math.floor(Math.random() * count);
  const randomDoc = await this.findOne().skip(rand);
  return randomDoc;
};