dongningce9075 2015-07-29 21:41
浏览 160
已采纳

PHP MongoDB使用循环插入

I am having some trouble working with MongoDB in PHP at the moment.

I am pulling records of financial data from a CSV file almost a gig, I am looping through the file fine and outputting and parsing the array.

During the while loop I am also trying to insert the data in to MongoDB

// Increase timeout on php script
ini_set('max_execution_time', 600);

while (($data = fgetcsv($file, 0, ",")) !==FALSE) {

    $parsedData['name'] = $data['0'];
    $parsedData['email'] = $data['1'];
    $parsedData['phone'] = $data['2'];
    $parsedData['address'] = $data['3'];
    $parsedData['gender'] = $data['4'];

    $collection->insert($parsedData);

}

So the problem is that it inserts only one of the records or a few, I can't really say it seems quite random.

Any help here would be great.

Tests Completed

  • Running the same function while testing with mysql returned successful.
  • print_r($parsedData) displays desired values.
  • Wrapping $collection->insert in an if statement returns true
  • 写回答

1条回答 默认 最新

  • dougu5950 2015-07-30 08:34
    关注

    Okay so I managed to resolve this issue after reading more on some MongoDB documentation.

    1. I wrapped the procedure with a try and catch adding an exception
    2. Added fsync and safe to the array that was sent to MongoDB
    3. The final piece added was "new MongoId" as MongoDB was returning duplicate _id (as far as I know this was the only necessary step to take)

      while (($data = fgetcsv($file, 0, ",")) !==FALSE) {
          try{ 
      
              // Add MongoId, without this it was returning a duplicate key 
              // error in the catch.
              $parsedData['_id'] =  new MongoId();
      
              $parsedData['name'] = $data['0'];
              $parsedData['email'] = $data['1'];
              $parsedData['phone'] = $data['2'];
              $parsedData['address'] = $data['3'];
              $parsedData['gender'] = $data['4'];
      
              // Submitted "safe" and "fsync" with the array, as far as I
              // can see MongoDB waits till data is entered before it sends
              // a true response instead of continuing after the function is 
              // executed.
              $collection->save($parsedData, array('safe' => true, 'fsync' => true));
      
      
          }catch(MongoCursorException $e){
              // This is where I caught the duplicate id
              print_r($e->doc['err']);
      
              // Kill the procedure
              die();
          }
      }
      

    If anyone can add to this it would be great as I thought Mongo generated its own id's and that it would only return true when data is entered or maybe I'm just expecting it to run similar to the MySQL drivers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大