PyMongo $in + $regex
Use python regular expressions. import re db.col.find({‘music_description’: {‘$in’: [ re.compile(‘.*heavy.*’), re.compile(‘.*metal.*’)]}})
Use python regular expressions. import re db.col.find({‘music_description’: {‘$in’: [ re.compile(‘.*heavy.*’), re.compile(‘.*metal.*’)]}})
I’ve ran into something similar, for me the problem was this line: from pymongo.objectid import ObjectId The call seems to work in versions of PyMongo < 2.2 In pymongo 2.2 the call to import objectid is: from bson.objectid import ObjectId
from pymongo import MongoClient client = MongoClient() db = client.test_database print(db.command(“collstats”, “test_collection”))
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
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
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
db.users.deleteMany({‘_id’:{‘$in’:inactive_users}})
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
The find method returns a Cursor instance, which allows you to iterate over all matching documents. To get the first document that matches the given criteria, you need to use find_one. The result of find_one is a dictionary. You can always use the list constructor to return a list of all the documents in the … Read more
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