dcdcdc
dcdcdc

Reputation: 263

PHP MongoDB insert using loop

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

Upvotes: 0

Views: 856

Answers (1)

dcdcdc
dcdcdc

Reputation: 263

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.

Upvotes: 3

Related Questions