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

How to delete a MongoDB collection in PyMongo

Sample code in Pymongo with comment as explanation: from pymongo import MongoClient connection = MongoClient(‘localhost’, 27017) #Connect to mongodb print(connection.database_names()) #Return a list of db, equal to: > show dbs db = connection[‘testdb1’] #equal to: > use testdb1 print(db.list_collection_names()) #Return a list of collections in ‘testdb1’ print(“posts” in db.list_collection_names()) #Check if collection “posts” # exists … Read more

PyMongo vs MongoEngine for Django

This is an old question but stumbling across it, I don’t think the accepted answer answers the question. The question wasn’t “What is MongoEngine?” – it was “Why should I use MongoEngine?” And the advantages of such an approach. This goes beyond Django to Python/Mongo in general. My two cents: While both PyMongo and MongoEngine … Read more

MongoDB return True if document exists

Note: This answer is outdated. More recent versions of MongoDB can use the far more efficient method db.collection.countDocuments. See the answer by Xavier Guihot for a better solution. find doesn’t return a boolean value, it returns a cursor. To check if that cursor contains any documents, use the cursor’s count method: if db.mycollection.find({‘UserIDS’: { “$in”: … Read more

How do I drop a MongoDB database using PyMongo?

PyMongo 2.4 up to at least 3.11.4 from pymongo import MongoClient client = MongoClient(‘<HOST>’, <PORT>) client.drop_database(‘<DBNAME>’) PyMongo Stable documentation PyMongo 3.2.1 documentation PyMongo 2.3 and earlier from pymongo import Connection connection = Connection(‘<HOST>’, <PORT>) connection.drop_database(‘<DBNAME>’) PyMongo 2.3 documentation PyMongo 1.0 documentation

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)