title: MongoTypeConversionException: Cannot cast STRING into a NullType with Mongo Spark Connector even when explicit schema does not contain NullTypes
body: <p>I'm importing a collection from MongodB to Spark.</p>
  <pre><code>val partitionDF = spark.read.format(&quot;com.mongodb.spark.sql.DefaultSource&quot;).option(&quot;database&quot;, &quot;db&quot;).option(&quot;collection&quot;, collectionName).load()
  </code></pre>
  <p>For the <code>data</code> column in the resulting <code>DataFrame</code>, I get this type:</p>
  <pre><code>StructType(StructField(configurationName,NullType,true), ...
  </code></pre>
  <p>so at least some types in some columns are <code>NullType</code>.</p>
  <p>As per <a href="https://stackoverflow.com/questions/68740925/writing-null-values-to-parquet-in-spark-when-the-nulltype-is-inside-a-structtype/68755724#68755724">Writing null values to Parquet in Spark when the NullType is inside a StructType</a> , I try fixing the schema by replacing all <code>NullType</code>s with <code>StringType</code>s:</p>
  <pre><code>def denullifyStruct(struct: StructType): StructType = {
    val items = struct.map{ field =&gt; StructField(field.name, denullify(field.dataType), field.nullable, field.metadata) }
    StructType(items)
  }
  
  def denullify(dt: DataType): DataType = {
    if (dt.isInstanceOf[StructType]) {
      val struct = dt.asInstanceOf[StructType]
      return denullifyStruct(struct)
    } else if (dt.isInstanceOf[ArrayType]) {
      val array = dt.asInstanceOf[ArrayType]
      return ArrayType(denullify(array.elementType), array.containsNull)
    } else if (dt.isInstanceOf[NullType]) {
      return StringType
    }
    return dt
  }
  
  val fixedDF = spark.createDataFrame(partitionDF.rdd, denullifyStruct(partitionDF.schema))
  </code></pre>
  <p>Issuing <code>fixedDF.printSchema</code> I can see that no <code>NullType</code> exists in the <code>fixedDF</code>'s schema anymore. But when I try to save it to Parquet</p>
  <pre><code>fixedDF.write.mode(&quot;overwrite&quot;).parquet(partitionName + &quot;.parquet&quot;)
  </code></pre>
  <p>I get the following error:</p>
  <pre><code>Caused by: com.mongodb.spark.exceptions.MongoTypeConversionException: Cannot cast STRING into a NullType (value: BsonString{value='117679.8'})
      at com.mongodb.spark.sql.MapFunctions$.convertToDataType(MapFunctions.scala:214)
      at com.mongodb.spark.sql.MapFunctions$.$anonfun$documentToRow$1(MapFunctions.scala:37)
      at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:238)
      at scala.collection.IndexedSeqOptimized.foreach(IndexedSeqOptimized.scala:36)
      at scala.collection.IndexedSeqOptimized.foreach$(IndexedSeqOptimized.scala:33)
      at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:198)
  </code></pre>
  <p>A <code>NullType</code> again!</p>
  <p>The same issue occurs when I just count the number of rows: <code>fixedDF.count()</code>.</p>
  <p>Does Spark infer the schema again when writing to Parquet (or counting)? Is it possible to turn such inference off (or overcome this in some other way)?</p>