title: Error: com.mongodb.MongoCursorNotFoundException: while reading from MongoDB into Spark
body: <p>I am trying to read a collection in MongoDB in Spark with 234 million records. I want only 1 field.</p>
  
  <pre><code>case class Linkedin_Profile(experience : Array[Experience])
  case class Experience(company : String)
  
  val rdd = MongoSpark.load(sc, ReadConfig(Map("uri" -&gt; mongo_uri_linkedin)))
  val company_DS = rdd.toDS[Linkedin_Profile]()
  val count_udf = udf((x: scala.collection.mutable.WrappedArray[String]) =&gt; {x.filter( _ != null).groupBy(identity).mapValues(_.size)})
  val company_ColCount = company_DS.select(explode(count_udf($"experience.company")))
  comp_rdd.saveAsTextFile("/dbfs/FileStore/chandan/intermediate_count_results.csv")
  </code></pre>
  
  <p>The job runs for 1 hour with half of jobs completed but after that it gives an error </p>
  
  <pre><code>com.mongodb.MongoCursorNotFoundException: 
  Query failed with error code -5 and error message 
  'Cursor 8962537864706894243 not found on server cluster0-shard-01-00-i7t2t.mongodb.net:37017' 
  on server cluster0-shard-01-00-i7t2t.mongodb.net:37017
  </code></pre>
  
  <p>I tried changing the configuration with below, but to no avail.</p>
  
  <pre><code>System.setProperty("spark.mongodb.keep_alive_ms", "7200000")
  </code></pre>
  
  <p>Please suggest how to read this large collection.</p>