How can I get N random documents from MongoDB (using Yii2)?
Is there some special method, like
Products::find()->random(20)->asArray()->all()
How can I get N random documents from MongoDB (using Yii2)?
Is there some special method, like
Products::find()->random(20)->asArray()->all()
Not in my knowledge. In one method you may make array using rand(0, array_count) on primary key and use "in".
That is not an option. Fetching everything from the database and then getting a random amount of data is not something you should be suggesting to anyone.
Thank you @samdark, but I know how to do it in a mongodb repl. What I’m asking is how to do it with Yii2. I saw there is an aggregate
function in Yii2’s MongoDB Collection class, but I couldn’t find any examples of how to use that.
Can’t help you with it since I’m not using MongoDB and haven’t developed the extension. I think it’s a good idea to dig into phpdoc/unit tests/code. If you’ll find out, maybe it’s worth contributing to docs.