Querying result from mongoose using dynamic model.find

前端 未结 2 1369
孤独总比滥情好
孤独总比滥情好 2021-01-06 15:22

I need to find the results of a query with mongoose find({}) method in Node.js with a variable containing model name.

var adSchema     =  new Sc         


        
相关标签:
2条回答
  • 2021-01-06 16:17

    Try this:

        var mongoose = require('mongoose');
        var Schema = mongoose.Schema;
        var anySchema = new Schema({
          fieldname:  String
        });
        var Test = mongoose.model('Test', anySchema);
        Test.find({}).exec(function(err,result){});
    
    0 讨论(0)
  • 2021-01-06 16:22

    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

    0 讨论(0)
提交回复
热议问题