Showing posts with label mongodb. Show all posts
Showing posts with label mongodb. Show all posts

Friday, September 19, 2014

Hacer una ordenacion aleatoria

Para ordenar de manera aleatoria en mongodb solamente se me ocurre insertar un campo aleatorio.


db.Collection
    .find({},{_id:1})
    .forEach(function(doc){
        db.collection.
        update(
            { _id: doc._id},
            { $set: { r: Math.random() } } ,
            { upsert: true }
                       );
    });
después solo tienes que ordenar por r.