Reputation: 59
I am looking to update approximately 3000K entries their distance field, with 4000 values through PyMongo bulkwrite(), as update_many single is too slow.
My practice:
operations = []
for i in data:
operations.append(updateMany({'location.zip':i['_id']}, {"$set":{'calculations.distance':i['distance']}}, upsert=True))
print(collection.bulk_write(operations, ordered=False))
where i = {'_id':'postal','distance':298.5}
This results in a
NameError: name 'updateMany' is not defined
When I check MongDB bulkWrite: It clearly shows:
An array of bulkWrite() write operations.
Valid operations are:
- insertOne
- updateOne
- updateMany
- deleteOne
- deleteMany
- replaceOne
Our dearly and beloved updateMany, which is written in 1000 ways in the past Mongo versions. However, is this not available anymore in PyMongo, and anyone know how to conquer this issue?
I have tried:
None have resulted in a good result.
Upvotes: 0
Views: 149
Reputation: 8814
You don't need to guess; just read the documentation.
Try:
from pymongo import MongoClient, UpdateMany
db = MongoClient()['mydatabase']
collection = db['collection']
data = [{'_id': 'postal', 'distance': 298.5}]
operations = []
for i in data:
operations.append(
UpdateMany({'location.zip': i['_id']}, {"$set": {'calculations.distance': i['distance']}}, upsert=True))
print(collection.bulk_write(operations, ordered=False))
Upvotes: 1