Random record from MongoDB

后端 未结 27 1984
栀梦
栀梦 2020-11-22 01:22

I am looking to get a random record from a huge (100 million record) mongodb.

What is the fastest and most efficient way to do so? The data is already t

27条回答
  •  别那么骄傲
    2020-11-22 01:47

    You can pick a random timestamp and search for the first object that was created afterwards. It will only scan a single document, though it doesn't necessarily give you a uniform distribution.

    var randRec = function() {
        // replace with your collection
        var coll = db.collection
        // get unixtime of first and last record
        var min = coll.find().sort({_id: 1}).limit(1)[0]._id.getTimestamp() - 0;
        var max = coll.find().sort({_id: -1}).limit(1)[0]._id.getTimestamp() - 0;
    
        // allow to pass additional query params
        return function(query) {
            if (typeof query === 'undefined') query = {}
            var randTime = Math.round(Math.random() * (max - min)) + min;
            var hexSeconds = Math.floor(randTime / 1000).toString(16);
            var id = ObjectId(hexSeconds + "0000000000000000");
            query._id = {$gte: id}
            return coll.find(query).limit(1)
        };
    }();
    

提交回复
热议问题