最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to rename collection which already exists in mongodb atlas? - Stack Overflow

programmeradmin2浏览0评论

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..!

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..!

Share Improve this question edited May 14, 2020 at 10:54 user125661 1,56812 silver badges28 bronze badges asked May 14, 2020 at 10:33 JIGNESH PATELJIGNESH PATEL 3975 silver badges20 bronze badges 1
  • 'I think I got my answer. please refer this link: stackoverflow./questions/54878935/…" – JIGNESH PATEL Commented May 14, 2020 at 11:00
Add a ment  | 

4 Answers 4

Reset to default 6

db.callhistories.renameCollection('call_history') use this to rename your existing collection and if you want to know more you can go to the official documentation https://docs.mongodb./manual/reference/method/db.collection.renameCollection/

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.

you can do :

 mongoose.model('callHistory', callHistorySchema, 'call_history'); 
 //Here 3rd argument 'call_history': as collection name

you can also do :

let callHistorySchema = mongoose.Schema({
  name: String,
  number: String.
  ....... 
}, { collection: 'call_history' });

Hope this will help you!

发布评论

评论列表(0)

  1. 暂无评论