title: Query a MongoDB collection using pyspark
body: <p>I am building an application utilizing PHP with  <strong>MongoDB</strong> as a database.One of collection across DB has massive volume of data i.e <strong>8GB</strong> data.
  I perform <strong>aggregate</strong> operation on data stored in MongoDB collection and accordingly generate statistics .
  But processing huge volume of data takes a long duration.Hence I opted for <a href="http://spark.apache.org/" rel="nofollow">Apache spark</a> to process data stored in <strong>MongDB collection</strong>
  I have configured <a href="https://docs.mongodb.com/spark-connector/" rel="nofollow">MongoDB spark connector</a> and executed a demo script in <strong>python</strong> to fetch data from mongo collection through spark.</p>
  
  <p>Following is python code snippet </p>
  
  <pre><code>from pyspark import SparkContext, SparkConf
  from pyspark.sql import SQLContext
  conf=SparkConf()
  conf.set('spark.mongodb.input.uri','mongodb://[host]/db.collection')
  conf.set('spark.mongodb.output.uri','mongodb://[host]/db.collection')
  sc = SparkContext(conf=conf)
  sqlContext = SQLContext(sc)
  df = sqlContext.read.format("com.mongodb.spark.sql.DefaultSource").load()
  df.printSchema()
  df.registerTempTable("mycollection")
  result_data=sqlContext.sql("SELECT * from mycollection limit 10")
  result_data.show()
  </code></pre>
  
  <p>In above code snippet I have utilized <a href="https://spark.apache.org/docs/1.6.1/api/python/pyspark.sql.html#module-pyspark.sql" rel="nofollow">pyspark.sql module</a> to generate <strong>RDD</strong>.
  But generation of <strong>RDD</strong> incurs reading of all data from collection which takes a long duration to read massive volume of data as opposed to principle on which <strong>Apache Spark</strong> works.
  Hence suggest me an appropriate solution to <strong>read data from Mongo collection using pyspark with optimal performance</strong> and also if any <strong>alternate package in Apache spark</strong> exists to <strong>communicate with MongoDB</strong>.</p>