'nestJS fetch data from a model dynamically i.e. model name as variable

How to fetch data from mongo model in nestjs only by name of the model.

i.e. instead of this.userModel.find() can we have some approach to get the model dynamically and then fetch data from there.

I tried,

const model = mongoose.model('User');
model.find({}); 

but it's not working

I have a requirement, where based on the input variable I have to get data of that collection.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source