Eliminar colecciones en mongodb

Buen día,

Como puedo eliminar/modificar una colección, que por error su nombre fue mal escrito con espacios, caracteres, etc.

Ejemplo:
Nombre de una colección con espacios
usuarios Doc
SesionesDoc Tx {1};

Hello @edith_t ,

Welcome to The MongoDB Community Forums! :wave:

I tried translating your question in Google translate and please correct me if my understanding of your use-case is wrong but I think you are trying to rename an existing collection in your database?

If that is right, then I believe you can use db.collection.renameCollection(). This method operates within a collection by changing the metadata associated with a given collection.

Example

Call the db.collection.renameCollection() method on a collection object. For example:

db.rrecord.renameCollection("record")

This operation will rename the rrecord collection to record .

Refer to the documentation renameCollection for additional warnings and messages.

Regards,
Tarun

1 Like

Hello @Tarun_Gaur

I tried change the name of the collection but show the next error:

db.Prueba uno.renameCollection(“prueba”);

Error: clone(t={}){const r=t.loc||{};return e({loc:new Position("line"in r?r.line:this.loc.line,"column"in r?r.column:……)} could not be cloned.

The problem is rename a collection when have space in the name, because does not allow me delete or modify this collection.

Hey @edith_t ,

To update such collection name, one can try using below code in mongosh

var authColl = db.getCollection("Prueba uno");
authColl.renameCollection("prueba");

Alternate way to do the same via query

db[“Prueba uno”].renameCollection(“prueba”)

Note: Please test the code in your test environment and update the code as per your requirements before making any changes to production environment.

Tarun

2 Likes

Thank you very much, this query was just what I needed, it helped me to solve my doubt.

Regards.

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.