Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (50.2k points)
edited by

I want to list all the data from a collection in a database in mongodb:

From the following requests:

http://localhost:3000/listdoc?model=Organization

I am implementing the following code :

exports.listDoc = function(req, res) {    

var Model = mongoose.model(req.query.model); //This is defined and returns my desired model name

        Model.find().populate('name').exec(function(err, models) {

            if (err) {

                res.render('error', {

                    status: 500

                });

            } else {

                res.jsonp(models);

            }

        });

};

I already have my entry in the database But the above code returns empty. I don't know why?

The schema:

var Organization = mongoose.Schema({

  name: String

});

1 Answer

0 votes
by (108k points)

I think that mongoose is making multiple collections. Mongoose is querying "organizations" collection but your data is in mongodb as "organization".So now either you can rename your collection or just notify to mongoose about it:

var schema = new Schema({ name: String }, { collection: 'actor' });

// or

schema.set('collection', 'actor');

// or

var collectionName = 'actor'

var M = mongoose.model('Actor', schema, collectionName)

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jan 5, 2020 in Web Technology by ashely (50.2k points)
0 votes
2 answers
0 votes
2 answers

Browse Categories

...