How to continue insertion after duplicate key error using PyMongo

You need to use insert_many method and set the ordered option to False. db_stock.insert_many(<list of documents>) As mentioned in the ordered option documentation: ordered (optional): If True (the default) documents will be inserted on the server serially, in the order provided. If an error occurs all remaining inserts are aborted. If False, documents will be … Read more

Using .sort with PyMongo

sort should be a list of key-direction pairs, that is db.test.find({“number”: {“$gt”: 1}}).sort([(“number”, 1), (“date”, -1)]) The reason why this has to be a list is that the ordering of the arguments matters and dicts are not ordered in Python < 3.6

How to get ordered dictionaries in pymongo?

This solution above is correct for older versions of MongoDB and the pymongo driver but it no longer works with pymongo3 and MongoDB3+ You now need to add document_class=OrderedDict to the MongoClient constructor. Modifying the above answer for pymongo3 compatibility. from collections import OrderedDict from pymongo import MongoClient import bson client = MongoClient(document_class=OrderedDict) sample_db = … Read more

JSON ValueError: Expecting property name: line 1 column 2 (char 1)

I encountered another problem that returns the same error. Single quote issue I used a json string with single quotes : { ‘property’: 1 } But json.loads accepts only double quotes for json properties : { “property”: 1 } Final comma issue json.loads doesn’t accept a final comma: { “property”: “text”, “property2”: “text2”, } Solution: … Read more

Getting Spark, Python, and MongoDB to work together

Updates: 2016-07-04 Since the last update MongoDB Spark Connector matured quite a lot. It provides up-to-date binaries and data source based API but it is using SparkConf configuration so it is subjectively less flexible than the Stratio/Spark-MongoDB. 2016-03-30 Since the original answer I found two different ways to connect to MongoDB from Spark: mongodb/mongo-spark Stratio/Spark-MongoDB … Read more