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