初始化后更改 Typegoose 集合名称

Change Typegoose collection name after initialize

我正在使用 typegoose 创建模型。在创建模型的过程中,我发现可以提供集合名称。但是一旦分配,我就找不到修改它的方法了。

export const MyModel: ModelType<MyModel> = new MyModel().setModelForClass(MyModel, {
    existingMongoose: mongoose,
    schemaOptions: {collection: 'my_collection_name'}
});

所以在上面 MyModel 中,我想更改我导入的集合名称。

如何更改模型中的集合名称?还是我只能选择在我想使用它的地方创建这个模型?

没关系。我只需要制作导出对象的功能。所以我将其更改为以下内容,以便我可以在使用此模型的地方传递 collectionName。

const DocumentFieldBooleanValueModel = (collectionName: string) : ReturnModelType<typeof DocumentFieldBooleanValue, BeAnObject> => {
  return getModelForClass(DocumentFieldBooleanValue, {
    schemaOptions: { collection: collectionName },
  });
};

export { DocumentFieldBooleanValueModel };

所以现在上面导出的模型函数我可以使用如下。

DocumentFieldBooleanValueModel('MyCustomCollectionName')

并且它会给出相同类型的鹅模型。