I have a MongoDB collection that looks something like this:
{
"_id" : { "customerName" : "Bob", "customerPhone" : "123-456-7890"},
"purchases": ["A", "B", "C", "D"]
}
Basically the _id is a pair of unique key about the customer and the purchases are the array of the items that the customer has bought.
I also have a PySpark DataFrame that I would like to push into this collection where it contains information that I would like to update this particular document.
df.write.format("com.mongodb.spark.sql.DefaultSource").mode("append") \
.option("spark.mongodb.output.uri", "mongodb://localhost:27017/customer.purchases").save()
The problem is that if I am updating this document where I want to add new purchases for Bob it would only append the non-existing ones in purchases instead of appending all.
As a result what I ended up doing right now is that I would just have to call rdd.collect() to turn the whole thing into a list and not using the schema to convert it to DataFrame. Then insert everything one by one while checking if the key exists; which makes this part slow and requires a lot of memory when the query for RDD gets large.
For versions:
PySpark: 2.2 MongoDB: 3.0.15 Mongo Spark Connector: 2.2.1
Does anyone if there is anything that I can do to append all element in the array to MongoDB collection using dataframe? Also, if there is anything that I am missing or other things that I should do please let me know. Thanks!