如何在 MongoDB Atlas 中重命名已经存在的集合?

5

我在MongoDB Atlas中有一个名为callhistories的集合,我想将其重命名为call_history。如果我在mongoose模型中添加如下的第三个选项:

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

我在MongoDB Atlas中更改我的集合名称,会对我的网站造成破坏吗?

请帮忙..!


我想我得到了答案。 请参考此链接:https://dev59.com/DrLma4cB1Zd3GeqPVx-f" - JIGNESH PATEL
4个回答

5

2
您可以使用MongoDB的renameCollection函数来更改集合名称。
例如:
const callHistory = mongoose.model('callHistory', callHistorySchema);
callHistory.renameCollection('call_history');

2

如果使用mongodb nodejs驱动程序,请使用此方法:

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

如果您的集合名称中包含特殊符号(如上面的示例),则之前的答案将无法正常工作。


1

以下是您可以做的事情:

 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' });

希望这能帮到你!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接