Dillip Kumar Pradhan Dillip Kumar Pradhan - 1 year ago 103
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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download