1

I have a collection name StudentMarks with 20000 records in my MongoDB. I am running a script to update a field in all documents of this collection. I am getting following error after it process 5000 records. I am unable to find out why this is happening and how to resolve it.

Error Is : Mongo Cursor Could not found on line 1125

Following code I have written in controller

public function updateAction(Request $request) {
    $examStudentMarkDocument11 = $schoolDM - > getRepository('EduExamCbseBundle:ExamCbseStudentSubjectMark') - > findAll();

    if (count($examStudentMarkDocument11)) {
    $i = 0;
    // MongoCursor::$timeout = -1;
    foreach($examStudentMarkDocument11 as $stdnt) {

        $id = $stdnt - > getId();
        $examStudentMarkDocument1 = $schoolDM - > getRepository('EduExamCbseBundle:ExamCbseStudentSubjectMark') - > find($id);

        $examStudentMarkDocument1 - > setCurrentSession($currentSessionYear); //Updating field here
        $schoolDM - > persist($examStudentMarkDocument1);
        $schoolDM - > flush();
        $i++;
    }
    }
}
Matteo
  • 37,680
  • 11
  • 100
  • 115
Sunil Rawat
  • 709
  • 10
  • 41
  • Hi @user3458514 have you find a solutions? What d you think about my solution? – Matteo Jul 30 '15 at 19:27
  • 1
    Hi Thanks Matto for your suggestion. its working now I just keep it like before just put one statment again at last : $schoolDM - > clear(); – Sunil Rawat Jul 31 '15 at 04:26

1 Answers1

0

I suggest you to do Updating multiple documents with a querybuilder instead of flush every single document.

So, as example, you can do the following statement:

$this->get('doctrine_mongodb')
    ->getManager()
    ->createQueryBuilder('EduExamCbseBundle:ExamCbseStudentSubjectMark')
    ->update()
    ->multiple(true)
    ->field('currentSession')->set($currentSessionYear)
//    ->field('username')->equals('sgoettschkes')// optional where conditions
    ->getQuery()
    ->execute();

Better move this method inside a custom repository class.

Hope this help

Matteo
  • 37,680
  • 11
  • 100
  • 115