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
4 Answers
Reset to default 6db.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!