Rename this collection. If operating in auth mode, client must be authorized as an admin to perform this operation. Raises :class:`TypeError` if `new_name` is not an instance of :class:`str`. Raises :class:`~pymongo.errors.InvalidName` if `new_name` is not a
(
self,
new_name: str,
session: Optional[AsyncClientSession] = None,
comment: Optional[Any] = None,
**kwargs: Any,
)
| 3101 | |
| 3102 | @_csot.apply |
| 3103 | async def rename( |
| 3104 | self, |
| 3105 | new_name: str, |
| 3106 | session: Optional[AsyncClientSession] = None, |
| 3107 | comment: Optional[Any] = None, |
| 3108 | **kwargs: Any, |
| 3109 | ) -> MutableMapping[str, Any]: |
| 3110 | """Rename this collection. |
| 3111 | |
| 3112 | If operating in auth mode, client must be authorized as an |
| 3113 | admin to perform this operation. Raises :class:`TypeError` if |
| 3114 | `new_name` is not an instance of :class:`str`. |
| 3115 | Raises :class:`~pymongo.errors.InvalidName` |
| 3116 | if `new_name` is not a valid collection name. |
| 3117 | |
| 3118 | :param new_name: new name for this collection |
| 3119 | :param session: a |
| 3120 | :class:`~pymongo.asynchronous.client_session.AsyncClientSession`. |
| 3121 | :param comment: A user-provided comment to attach to this |
| 3122 | command. |
| 3123 | :param kwargs: additional arguments to the rename command |
| 3124 | may be passed as keyword arguments to this helper method |
| 3125 | (i.e. ``dropTarget=True``) |
| 3126 | |
| 3127 | .. note:: The :attr:`~pymongo.asynchronous.collection.AsyncCollection.write_concern` of |
| 3128 | this collection is automatically applied to this operation. |
| 3129 | |
| 3130 | .. versionchanged:: 3.6 |
| 3131 | Added ``session`` parameter. |
| 3132 | |
| 3133 | .. versionchanged:: 3.4 |
| 3134 | Apply this collection's write concern automatically to this operation |
| 3135 | when connected to MongoDB >= 3.4. |
| 3136 | |
| 3137 | """ |
| 3138 | if not isinstance(new_name, str): |
| 3139 | raise TypeError(f"new_name must be an instance of str, not {type(new_name)}") |
| 3140 | |
| 3141 | if not new_name or ".." in new_name: |
| 3142 | raise InvalidName("collection names cannot be empty") |
| 3143 | if new_name[0] == "." or new_name[-1] == ".": |
| 3144 | raise InvalidName("collection names must not start or end with '.'") |
| 3145 | if "$" in new_name and not new_name.startswith("oplog.$main"): |
| 3146 | raise InvalidName("collection names must not contain '$'") |
| 3147 | |
| 3148 | new_name = f"{self._database.name}.{new_name}" |
| 3149 | cmd = {"renameCollection": self._full_name, "to": new_name} |
| 3150 | cmd.update(kwargs) |
| 3151 | if comment is not None: |
| 3152 | cmd["comment"] = comment |
| 3153 | write_concern = self._write_concern_for_cmd(cmd, session) |
| 3154 | client = self._database.client |
| 3155 | |
| 3156 | async def inner( |
| 3157 | session: Optional[AsyncClientSession], conn: AsyncConnection, _retryable_write: bool |
| 3158 | ) -> MutableMapping[str, Any]: |
| 3159 | return await conn.command( |
| 3160 | "admin", |
nothing calls this directly
no test coverage detected