def __find_and_modify(self, filter: dict, projection: Optional[Union[list, dict]],
sort: Optional[List[tuple]], upsert: Optional[bool] = None,
return_document: bool = ReturnDocument.BEFORE, **kwargs) -> MutableMapping:
"""Internal findAndModify helper."""
common.validate_is_mapping('filter', filter)
if not isinstance(return_document, bool):
raise ValueError('return_document must be ReturnDocument.BEFORE or ReturnDocument.AFTER')
cmd = SON([('findAndModify', self.name),
('query', filter),
('new', return_document)])
collation = validate_collation_or_none(kwargs.pop('collation', None))
cmd.update(kwargs)
if projection is not None:
cmd['fields'] = helpers._fields_list_to_dict(projection, 'projection')
if sort is not None:
cmd['sort'] = helpers._index_document(sort)
if upsert is not None:
common.validate_boolean('upsert', upsert)
cmd['upsert'] = upsert
connection = await self.database.client.get_connection()
if connection.max_wire_version >= 4 and 'writeConcern' not in cmd:
wc_doc = self.write_concern.document
if wc_doc:
cmd['writeConcern'] = wc_doc
out = await connection.command(
self.database.name, cmd, ReadPreference.PRIMARY, self.codec_options,
allowable_errors=[_NO_OBJ_ERROR], collation=collation
)
helpers._check_write_command_response([(0, out)])
return out.get('value')
评论列表
文章目录