[英]How can I shorten page references from specific to a range (eg. pg 1-3,6-7,9 to pg 1-9)
[英]How can I get data from a specific row (eg. 6th row) in a mongoDB collection?
如果我在mongo集合中的數據是這樣的
{ "_id" : ObjectId("4fff9946af8c6149aed251ab"), "subject_slug" : "math", "lesson_slug" : "mat01" }
{ "_id" : ObjectId("4fff9946af8c6149aed251ac"), "subject_slug" : "math", "lesson_slug" : "mat02" }
{ "_id" : ObjectId("4fff9946af8c6149aed251ad"), "subject_slug" : "math", "lesson_slug" : "mat03" }
{ "_id" : ObjectId("4fff9946af8c6149aed251ae"), "subject_slug" : "eng", "lesson_slug" : "eng01" }
{ "_id" : ObjectId("4fff9946af8c6149aed251af"), "subject_slug" : "eng", "lesson_slug" : "eng02" }
{ "_id" : ObjectId("4fff9946af8c6149aed251b0"), "subject_slug" : "eng", "lesson_slug" : "eng03" }
{ "_id" : ObjectId("4fff9946af8c6149aed251b1"), "subject_slug" : "phy", "lesson_slug" : "phy01" }
{ "_id" : ObjectId("4fff9946af8c6149aed251b2"), "subject_slug" : "phy", "lesson_slug" : "phy02" }
{ "_id" : ObjectId("4fff9946af8c6149aed251b3"), "subject_slug" : "phy", "lesson_slug" : "phy03" }
{ "_id" : ObjectId("4fff9946af8c6149aed251b4"), "subject_slug" : "chem", "lesson_slug" : "che01" }
{ "_id" : ObjectId("4fff9946af8c6149aed251b5"), "subject_slug" : "chem", "lesson_slug" : "che02" }
{ "_id" : ObjectId("4fff9946af8c6149aed251b6"), "subject_slug" : "chem", "lesson_slug" : "che03" }
{ "_id" : ObjectId("4fff9946af8c6149aed251b7"), "subject_slug" : "chem", "lesson_slug" : "che04" }
如果我想要第6行中的數據而沒有循環數據。 php中是否有短代碼來獲取該行中的數據?
為了找到第六條記錄,您需要使用一些MongoCursor方法 :
sort()
所以有一個特定的順序(例如_id
) skip()
跳過許多記錄(跳過5查找第6條記錄) limit()
限制一個結果 代碼示例:
<?php
$mongo = new Mongo();
$collection = $mongo->mydb->subject;
$query = array();
$sort = array('_id' => 1);
// Find the 6th record
$doc = $collection->find($query)->sort($sort)->skip(5)->limit(1)->getNext();
print_r($doc);
?>
樣本輸出:
Array
(
[_id] => MongoId Object
(
[$id] => 4fff9946af8c6149aed251b0
)
[subject_slug] => eng
[lesson_slug] => eng03
)
請注意,如果您需要跳過大量文檔,則skip()
的性能可能會成為問題。 如果可能的話,更有效的選擇是使用范圍查詢。
看起來像是json格式的內容,該如何將它們全部讀取到一個對象中,然后json_decode到另一個對象中,以便稍后您可以讀出第六行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.