我已访问 Facebook Graph API 以获取一个 JSON 对象,该对象代表我的提要(我的 Facebook 墙)上的最新帖子。然后,我使用 PHP Mongo 驱动程序将其保存到名为 feeds 的 MongoDB 集合中。
//$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() 等等......有些结构不同,因为有些包含 [story] 字段,其他包含 [message] 字段,有些包含两者。
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
)
)
问题是我不想只找到整个集合,我只想找到那些说 [message] 和 [story] 字段的数组,然后只找到它们的内容,别无其他。
我正在尝试接收一个子集,两个级别的深度:
//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...
)