MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / _maybe_add_read_preference

Function _maybe_add_read_preference

pymongo/message.py:121–136  ·  view source on GitHub ↗

Add $readPreference to spec when appropriate.

(
    spec: MutableMapping[str, Any], read_preference: _ServerMode
)

Source from the content-addressed store, hash-verified

119
120
121def _maybe_add_read_preference(
122 spec: MutableMapping[str, Any], read_preference: _ServerMode
123) -> MutableMapping[str, Any]:
124 """Add $readPreference to spec when appropriate."""
125 mode = read_preference.mode
126 document = read_preference.document
127 # Only add $readPreference if it's something other than primary to avoid
128 # problems with mongos versions that don't support read preferences. Also,
129 # for maximum backwards compatibility, don't add $readPreference for
130 # secondaryPreferred unless tags or maxStalenessSeconds are in use (setting
131 # the secondaryOkay bit has the same effect).
132 if mode and (mode != ReadPreference.SECONDARY_PREFERRED.mode or len(document) > 1):
133 if "$query" not in spec:
134 spec = {"$query": spec}
135 spec["$readPreference"] = document
136 return spec
137
138
139def _convert_exception(exception: Exception) -> dict[str, Any]:

Calls

no outgoing calls