[英]How to rename collection which already exists in mongodb atlas?
我在 MongoDB 地图集中有一个名为callhistories
的集合,我想将其重命名为call_history
。 如果我在我的 mongoose model 中添加第三个选项,如下所示:
const callHistory = mongoose.model("callHistory", callHistorySchema, 'call_history');
它会重命名我在 MongoDB 地图集中的收藏,还是会破坏我的网站?
请帮忙..!
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.com/manual/reference/method/db.collection .renameCollection/
您可以使用 MongoDB 的 renameCollection function 来更改集合名称。
例如:-
const callHistory = mongoose.model('callHistory', callHistorySchema);
callHistory.renameCollection('call_history');
如果使用 mongodb nodejs 驱动程序,请使用此选项:
db.collection(':podCasts').rename('podCasts');
如果您有一个特殊符号(如上面的示例)作为您的集合名称,则以前的答案将不起作用。
你可以做:
mongoose.model('callHistory', callHistorySchema, 'call_history');
//Here 3rd argument 'call_history': as collection name
你也可以这样做:
let callHistorySchema = mongoose.Schema({
name: String,
number: String.
.......
}, { collection: 'call_history' });
希望对你有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.