javascript 来自 Mongoose 集合的随机文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14644545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Random document from a collection in Mongoose
提问by user1680104
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 中使用它。
回答by matthewtole
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
NBI modified the code a bit to make it more readable.
注意我稍微修改了代码以使其更具可读性。
回答by Eat at Joes
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
});
});
回答by Mihai Tomescu
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。
回答by Ioanna
A shorter and maybemore performant solution
(we don't iterate through the collection once to count and a second time to skip elements, but mongoose might do that behind the scenes):
一个更短但可能更高效的解决方案
(我们不会遍历集合一次来计数和第二次跳过元素,但猫鼬可能会在幕后这样做):
使用聚合和 $sample:
Model.aggregate([{ $sample: { size: 1 } }])
回答by martinho
You can use aggregate:
您可以使用聚合:
User.aggregate([
{$match: {gender: "male"}},
{$sample: {size: 10}}
], function(err, docs) {
console.log(docs);
});
Or you can use npm package https://www.npmjs.com/package/mongoose-simple-random
或者你可以使用 npm 包https://www.npmjs.com/package/mongoose-simple-random
User.findRandom({gender: "male"}, {}, {limit: 10}, function(err, results) {
console.log(results); // 10 elements
});
回答by embiem
For people looking at this in times of async/await, promises etc.:
对于在 async/await、promise 等情况下查看此内容的人:
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;
};