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 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行