簡體   English   中英

如何重命名mongodb圖集中已經存在的集合?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM