title: Read Data from MongoDB through Apache Spark with a query
body: <p>I am able to read the data stored in MongoDB via Apache Spark via the conventional methods described in its documentation. I have a mongoDB query that I would like to be used while loading the collection. The query is simple, but I can't seem to find the correct way to specify the query the <code>config()</code> function in SparkSession object.</p>
  
  <p>Following is my SparkSession builder</p>
  
  <pre><code>val confMap: Map[String, String] = Map(
        "spark.mongodb.input.uri" -&gt; "mongodb://xxx:xxx@mongodb1:27017,mongodb2:27017,mongodb3:27017/?ssl=true&amp;replicaSet=MongoShard-0&amp;authSource=xxx&amp;retryWrites=true&amp;authMechanism=SCRAM-SHA-1",
        "spark.mongodb.input.database" -&gt; "A",
        "spark.mongodb.input.collection" -&gt; "people",
        "spark.mongodb.output.database" -&gt; "B",
        "spark.mongodb.output.collection" -&gt; "result",
        "spark.mongodb.input.readPreference.name" -&gt; "primaryPreferred"
      )
      conf.setAll(confMap)
      val spark: SparkSession =
        SparkSession.builder().master("local[1]").config(conf).getOrCreate()
  </code></pre>
  
  <p>Is there a way to specify the MongoDB query in the SparkConf object so that the SparkSession reads only the specific fields present in the collection.</p>