こんにちは、コレクションからランダムな 6 行を表示したいと思います。各行をタイムスタンプとして使用できるので、それを使用できますが、コレクションから6行のみを返してランダムにする方法が質問です
これが私のコレクションのサンプルです - 私はPHPを使用しています
{
"age": "2",
"breed": "Bengal",
"dislikes": "Dislikes being patted by people",
"likes": "Like to purr and get headbutts. Sleeps on our bed, with Woody our dog, and also comes in for food at 6pm, loves Tin fish and is known to meow quite lo [...]",
"lost": true,
"pet_lost_date": NumberInt(1361366445),
"type": "cat"
}
この db.items.find().skip(randonNumberHere).limit(1); を見ました。- MongoDB: コレクションから複数のランダム ドキュメントをプルする
しかし、私はそれを理解していませんでした。それから理解できるのは、行数をスキップするすべてのものを見つける find() と、返される行数である limit() だけです。
ただし、私の質問は、失われたペットをすべて取得してランダムにし、6つだけを表示することです
public function lost_pets($no){
$collection = static::db()->ipet_mypet;
$pet = $collection->find(array('lost': true, '$where'=> function(){var randomNumber=Math.random(); return this.random>=randomNumber || this.random>randomNumber })).sort(array('pet_lost_date'=> 1)).limit(6);
}