Dillip Kumar Pradhan Dillip Kumar Pradhan - 2 months ago 13x
Node.js Question

Querying result from mongoose using dynamic model.find

I need to find the results of a query with mongoose

method in Node.js with a variable containing model name.

var adSchema = new Schema({ schema defination });
var Ad = mongoose.model('Ad', adSchema);
var variableName = 'Ad';
variableName.find({}).exec(function (err, adObj) {});

Is it possible or not?

Thanks in advance


You should be able to do that when calling model with just the name like so

mongoose.model('Ad').find({}).exec(function (err, adObj) {});  

See here for the corresponding part of the official docs