0

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

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

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

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

4

1 回答 1

0

没关系。我只需要制作导出对象的功能。所以我把它改成了下面,这样我就可以在我使用这个模型的地方传递collectionName。

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

export { DocumentFieldBooleanValueModel };

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

DocumentFieldBooleanValueModel('MyCustomCollectionName')

它会给出相同的 typegoose 模型。

于 2021-04-05T18:20:58.960 回答