我訪問了Facebook Graph API以獲取代表我的訂閱源(我的Facebook牆)上的最新帖子的JSON對象。然後,我使用PHP Mongo Driver將它保存到一個名爲feeds的MongoDB集合中。使用PHP驅動程序深入查詢MongoDB子集兩級深入使用PHP驅動程序
//$post['feed']['data'] contains the Facebook JSON object of wall posts
//create a mongo instance
$mongo = new Mongo();
//access the feeds collection
$feeds = $mongo->changeup->feeds;
//dump the feed right into mongo
$feeds->insert($post['feed']['data']);
這是在讀回被放入mongo中的整個對象之後,其中一個數組看起來像。
我只向你展示一個,但它給了我多個,每個索引,下一個是[1] => Array()等等...一些結構不同,因爲一些包含[故事]字段,其他字段包含[消息]字段,有些字段包含兩者。
Query:
$cursor = $feeds->find();
foreach ($cursor as $feed) {
print_r($feed);
}
Result:
[0] => Array
(
[id] => 505212695_10150696450097696
[from] => Array
(
[name] => John Doe
[id] => 505212695
)
[story] => "Text of a story I posted on my wall..."
[story_tags] => Array
(
[38] => Array
(
[0] => Array
(
[id] => 15212444
[name] => John Doe
[offset] => 38
[length] => 10
[type] => user
)
)
)
[type] => status
[application] => Array
(
[name] => Share_bookmarklet
[id] => 5085647995
)
[created_time] => 2012-04-04T05:51:21+0000
[updated_time] => 2012-04-04T05:51:21+0000
[comments] => Array
(
[count] => 0
)
)
的問題是,我不想隨便找整個集合,我想找到只有那些說[消息]這些陣列和[故事]字段,然後隨便找個它們的內容並沒有什麼其他。
我想接受一個子集,兩層深:
//this works, however, I'm only able to get the 0 array
$cursor = $feeds->find(array(), array('0.story' => true));
如何通過所有陣列過濾器?
我希望我的最終結果是這樣的:
Array
(
[_id] => MongoId Object
(
[$id] => 4f7db4dd6434e64959000000
)
[0] => Array
(
[story] => "Text of a story I posted on my wall..."
)
[1] => Array
(
[story] => "Text of a story I posted on my wall..."
)
[2] => Array
(
[story] => "Text of a story I posted on my wall..."
[message] => "In this case message text exists as well..."
)
[3] => Array
(
[message] => "Text of a message I posted on my wall..."
)
etc...
)
非常感謝,作品! – Allen 2012-04-05 23:24:58