7

I have following bulk_write to upsert each document from my dataset to a collection.

data = [] # list of dicts/documents
mongo = MongoClient('some_host')
db = mongo['some_db']
collection = db['some_collection']
operations = [UpdateOne({'_id': d['_id']}, {'$set': d}, upsert=True) for d in data]
result = collection.bulk_write(operations)

It runs fine running on a local MongoDB server but I am getting following error message when running on AWS DocumentDB. There's a way around which is I delete and insert each record, but want to understand why this happens and use Update instead of Delete+Insert

pymongo.errors.OperationFailure: Retryable writes are not supported, full error: {'ok': 0.0, 'code': 301, 'errmsg': 'Retryable writes are not supported', 'operationTime': Timestamp(1638883052, 1)}

learningisfun
  • 131
  • 1
  • 5

4 Answers4

8

Amazon DocumentDB does not currently support retryable writes so they need to be disabled with retryWrites=False since they are enabled by default by the driver as explained here: https://docs.aws.amazon.com/documentdb/latest/developerguide/functional-differences.html#functional-differences.retryable-writes

R2D2
  • 9,410
  • 2
  • 12
  • 28
0

As per this answer:

Mongoose findOneAndUpdate throw "Retryable writes are not supported" error when working with aws Documentdb

retryWrites=False, adding this to MongoClient worked :)

Matt Andruff
  • 4,974
  • 1
  • 5
  • 21
learningisfun
  • 131
  • 1
  • 5
  • retryWrites=False Adding this to the mongo connection url as a query param worked for me as my java project is the mongo client. – veritas Jun 09 '23 at 04:56
0

I was able to deal with this by disabling retryable writes with Rails 7.0.5 and mongoid 8.0.3 by adding retry_writes: false in the options stanza for my DB config in mongoid.yml

Wedge Martin
  • 777
  • 6
  • 15
0

So, Retryable writes allow MongoDB drivers to automatically retry certain write operations a single time if they encounter network errors. However DocumentDB keeps its enabled by default and to disable you need to modify the connection string and include &retryWrites=False.

from typing import Dict
from pymongo import MongoClient


DATABASE = "SampleDatabase"

def initialize_conn() -> Dict:
    CONNECTION_STRING = f"mongodb://{username}:{password}@{host}/?ssl=true&ssl_ca_certs=global-bundle.pem&retryWrites=false"
    mongo = MongoClient(CONNECTION_STRING)[DATABASE]

    return {"MongoClient": mongo}

Learn More about Retriable Writes here: https://www.mongodb.com/docs/v5.0/core/retryable-writes/#retryable-writes