我试图将$ json插入MongoDB。我的想法是,对数组进行预处理/循环以仅获得'状态'。
$ json从另一个函数传递。看起来像这样:
{
"statuses": [
{
"created_at": "Fri Apr 22",
"id": 123,
"text": "Sample text 1",
},
{
"created_at": "Fri Apr 22",
"id": 321,
"text": "Sample text 1",
}
],
"search_metadata": {
"count": 2,
"since_id": 0,
"since_id_str": "0"
}
}
代码
<?php
$m = new MongoClient();
$db = $m->selectDB("newDB");
$collection = new MongoCollection($db, 'newStatus');
// looping through $json
foreach ($json as $document){
// another looping to get 'statuses'
foreach($document['statuses'] as $status) {
// store into MongoDB
$collection->insert(json_decode($status));
}
}
?>
修订:以上代码有效!添加$collection->insert(json_decode($document));
然而,它存储了整个数组块。我只想要&#39;状态&#39;。