Javascript 为什么node.js方法的findRandom()mongoose不起作用?

Javascript 为什么node.js方法的findRandom()mongoose不起作用?,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,模式: 调用FindAndom时,控制台显示[] var random = require('mongoose-random'); var productSchema = new mongoose.Schema(...) productSchema.plugin(random, {path: 'r'}); var products = mongoose.model('sportsStore', productSchema); module.exports = products; “查找”方

模式:

调用FindAndom时,控制台显示[]

var random = require('mongoose-random');

var productSchema = new mongoose.Schema(...)
productSchema.plugin(random, {path: 'r'});

var products = mongoose.model('sportsStore', productSchema);
module.exports = products;
“查找”方法工作正常。

尝试更改行

getRandomProducts: function (count) {
    products.findRandom().limit(count).exec(function(error, result) {
        console.log(result);  // -> []
    })


未经测试,但应能正常工作。

解决方案可能就在这里-请参阅示例中的注释

productSchema.plugin(random());
试一试

productSchema.plugin(random());
// if you have an existing collection, it must first by synced.
// this will add random data for the `path` key for each doc.
productSchema.syncRandom(function (err, result) {
   console.log(result.updated);
});