How to rename collection which already exists in mongodb atlas?

Viewed 2896

I have a collection in MongoDB atlas named callhistories I want to rename it as call_history. If I add the third option in my mongoose model as below :

const callHistory = mongoose.model("callHistory", callHistorySchema, 'call_history');

Would it rename my collection in MongoDB atlas or it will break my site?

Please Help..!

3 Answers

You can use the renameCollection function of MongoDB to change the collection name.

for eg:-

const callHistory = mongoose.model('callHistory', callHistorySchema);
callHistory.renameCollection('call_history');

Use this if using mongodb nodejs driver:

db.collection(':podCasts').rename('podCasts');

The previous answers won't work if you have a special symbol (like above example) as your collection name.

Related