Giter Club home page Giter Club logo

Comments (4)

larryprice avatar larryprice commented on June 24, 2024 1

OK I've started looking into this, and although we get a significant slowdown, I believe that having more random docs will be better. Will update in a few days once I've got the algo down.

from mongoose-simple-random.

larryprice avatar larryprice commented on June 24, 2024

I think that's how it currently works, and I agree that it's probably non-optimal as far as functionality goes. I'll happily look at any submitted PR.

from mongoose-simple-random.

joeytwiddle avatar joeytwiddle commented on June 24, 2024

No solution here I'm afraid. The approach I landed on was to choose one random document n times, using your skip approach, and then handle the risk of the same document being chosen twice. That's n queries!

From the research I did: an option some people used was to include a random value in a field of every document in the DB, and then use that field for ordering. But it seems to me that those values should be updated each time (at least for the retrieved documents), otherwise later queries may see similar repeated blocks of documents.

I wonder if a mongo aggregate query could somehow hash (the objid of each document combined with a fresh random value supplied each time). In theory that could provide a random an unpredictable value we could sort on. It might be pretty heavy on large collections though.

from mongoose-simple-random.

lchenay avatar lchenay commented on June 24, 2024

I don't use this plugin due to sequential issue.

I have quick coded this and it works like wanted.
It have 0 chance to take same documents twice (cf _.sample(_.range(count), nb);)

As I do use external dependancy as async and underscore, i can't make PR with this code.

var async = require("async");
var _ = require("underscore");

module.exports = exports = function(schema) {
    return schema.statics.pickRandom = function(query, nb, next) {
        return this.count(query, (err, count) => {
            if (err) {
                return next(err);
            }
            var randomSkip = _.sample(_.range(count), nb);
            return async.map(randomSkip, (skip, next) => {
                return this.findOne(query).skip(skip).exec(next);
            }, function(err, memories) {
                return next(err, memories, count);
            });
        });
    };
};

from mongoose-simple-random.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.