title: How to resolve com.mongodb.spark.exceptions.MongoTypeConversionException: Cannot cast... Java Spark
body: <p>Hi I am new to Java Spark, and have been looking for solutions for couple of days.</p>
  
  <p>I am working on loading MongoDB data into hive table, however, I found some error while saveAsTable that occurs this error</p>
  
  <pre><code>com.mongodb.spark.exceptions.MongoTypeConversionException: Cannot cast STRING into a StructType(StructField(oid,StringType,true)) (value: BsonString{value='54d3e8aeda556106feba7fa2'})
  </code></pre>
  
  <p>I've tried increase the sampleSize, different mongo-spark-connector versions, ... but non of working solutions.</p>
  
  <p>I can't figure out what is the root cause and what are the gaps in between that needs to be done?</p>
  
  <p>The most confusing part is I have similar sets of data using the same flow without issue.</p>
  
  <p>the mongodb data schema is like nested struct and array</p>
  
  <pre><code>root
   |-- sample: struct (nullable = true)
   |    |-- parent: struct (nullable = true)
   |    |    |-- expanded: array (nullable = true)
   |    |    |    |-- element: struct (containsNull = true)
   |    |    |    |    |-- distance: integer (nullable = true)
   |    |    |    |    |-- id: struct (nullable = true)
   |    |    |    |    |    |-- oid: string (nullable = true)
   |    |    |    |    |-- keys: array (nullable = true)
   |    |    |    |    |    |-- element: string (containsNull = true)
   |    |    |    |    |-- name: string (nullable = true)
   |    |    |    |    |-- parent_id: array (nullable = true)
   |    |    |    |    |    |-- element: struct (containsNull = true)
   |    |    |    |    |    |    |-- oid: string (nullable = true)
   |    |    |    |    |-- type: string (nullable = true)
   |    |    |-- id: array (nullable = true)
   |    |    |    |-- element: struct (containsNull = true)
   |    |    |    |    |-- oid: string (nullable = true)
  </code></pre>
  
  <p>sample data</p>
  
  <pre><code>    "sample": {
        "expanded": [
          {
            "distance": 0,
            "type": "domain",
            "id": "54d3e17b5cf737074d4065b0",
            "parent_id": [
              "54d3e1775cf737074d406599"
            ],
            "name": "level2"
          },
          {
            "distance": 1,
            "type": "domain",
            "id": "54d3e1775cf737074d406599",
            "name": "level1"
          }
        ],
        "id": [
          "54d3e17b5cf737074d4065b0"
        ]
      }
  </code></pre>
  
  <p>sample code</p>
  
  <pre><code>public static void main(final String[] args) throws InterruptedException {
      // spark session read mongodb
      SparkSession mongo_spark = SparkSession.builder()
              .master("local")
              .appName("MongoSparkConnectorIntro")
              .config("mongo_spark.master", "local")
              .config("spark.mongodb.input.uri", "mongodb://localhost:27017/test_db.test_collection")
              .enableHiveSupport()
              .getOrCreate();
  
      // Create a JavaSparkContext using the SparkSession's SparkContext object
      JavaSparkContext jsc = new JavaSparkContext(mongo_spark.sparkContext());
  
      // Load data and infer schema, disregard toDF() name as it returns Dataset
      Dataset&lt;Row&gt; implicitDS = MongoSpark.load(jsc).toDF();
      implicitDS.printSchema();
      implicitDS.show();
  
      // createOrReplaceTempView to see if the data being read
      // implicitDS.createOrReplaceTempView("my_table");
      // implicitDS.printSchema();
      // implicitDS.show();
  
      // saveAsTable
      implicitDS.write().saveAsTable("my_table");
      mongo_spark.sql("SELECT * FROM my_table limit 1").show();
  
      mongo_spark.stop();
  }
  </code></pre>
  
  <p>If anyone have some thoughts I would be very much appreciate.
  Thanks</p>