Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 73 lines (49 sloc) 2.079 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
================
renameCollection
================

.. default-domain:: mongodb

Definition
----------

.. dbcommand:: renameCollection

   Changes the name of an existing collection. Specify collections
   to :dbcommand:`renameCollection` in the form of a complete
   :term:`namespace`, which includes the database name. Issue the
   :dbcommand:`renameCollection` command against the :term:`admin
   database`. The command takes the following form:

   .. code-block:: javascript

      { renameCollection: "<source_namespace>", to: "<target_namespace>", dropTarget: <true|false> }

   The command contains the following fields:

   .. include:: /reference/command/renameCollection-field.rst

   :dbcommand:`renameCollection` is suitable for production
   environments; *however*:

   - :dbcommand:`renameCollection` blocks all database activity
     for the duration of the operation.

   - :dbcommand:`renameCollection` is **not** compatible with sharded
     collections.

   .. warning::

      :dbcommand:`renameCollection` fails if ``target`` is the name of
      an existing collection *and* you do not specify ``dropTarget:
      true``.

      If the :dbcommand:`renameCollection` operation does not complete
      the ``target`` collection and indexes will not be usable and
      will require manual intervention to clean up.

Exceptions
----------

:exception 10026:
   Raised if the ``source`` namespace does not exist.

:exception 10027:
   Raised if the ``target`` namespace exists and ``dropTarget`` is
   either ``false`` or unspecified.

:exception 15967:
   Raised if the ``target`` namespace is an invalid collection
   name.

Shell Helper
------------

The shell helper :method:`db.collection.renameCollection()` provides a
simpler interface to using this command within a database.
The following is equivalent to the previous example:

.. code-block:: javascript

   db.source-namespace.renameCollection( "target" )

.. warning:: You cannot use :dbcommand:`renameCollection` with
   sharded collections.

.. include:: /includes/warning-blocking-global.rst
Something went wrong with that request. Please try again.