$bulk= newMongoDBDriverBulkWrite(['ordered'=>true]);$bulk->delete([]);$bulk->insert(['_id'=>1]);$bulk->insert(['_id'=>2]);$bulk->insert(['_id'=>3,'hello'=>'world']);$bulk->update(['_id'=>3], ['$set'=> ['hello'=>'earth']]);$bulk->insert(['_id'=>4,'hello'=>'pluto']);$bulk->update(['_id'=>4], ['$set'=> ['hello'=>'moon']]);$bulk->insert(['_id'=>3]);$bulk->insert(['_id'=>4]);$bulk->insert(['_id'=>5]);$manager= newMongoDBDriverManager('mongodb://localhost:27017');$writeConcern= newMongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY,1000);
try {$result=$manager->executeBulkWrite('db.collection',$bulk,$writeConcern);
} catch (MongoDBDriverExceptionBulkWriteException $e) {$result=$e->getWriteResult();// Check if the write concern could not be fulfilledif ($writeConcernError=$result->getWriteConcernError()) {printf("%s (%d): %sn",$writeConcernError->getMessage(),$writeConcernError->getCode(),var_export($writeConcernError->getInfo(),true)
);
}// Check if any write operations did not complete at allforeach ($result->getWriteErrors() as$writeError) {printf("Operation#%d: %s (%d)n",$writeError->getIndex(),$writeError->getMessage(),$writeError->getCode()
);
}
} catch (MongoDBDriverExceptionException $e) {printf("Other error: %sn",$e->getMessage());
exit;
}printf("Inserted %d document(s)n",$result->getInsertedCount());printf("Updated %d document(s)n",$result->getModifiedCount());?>