我访问了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