35

i am working upon mongoose to list all the data from a collection in a db in mongodb:

from the requests:

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

i am doing 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 database But the above code returns empty. Why?

EDIT : the following code also returns empty:

exports.listDoc = function(req, res) {
    var Model = mongoose.model(req.query.model);
    Model.find({},function(err,models){
        console.log(models);
         if (err) {
            res.render('error', {
                status: 500
            });
        } else {
            res.jsonp(models);
        }
    });
};

schema used :

var Organization = mongoose.Schema({
  name: String
});
codeofnode
  • 18,169
  • 29
  • 85
  • 142

2 Answers2

99

Your problem is mongoose pluralizes collections. Mongoose is querying "organizations" but your data is in mongodb as "organization". Make them match and you should be good to go. You can either rename it in mongodb via the mongo shell or tell mongoose about it. From the mongoose docs:

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

// or

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

// or

var collectionName = 'actor'
var M = mongoose.model('Actor', schema, collectionName)
Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • 1
    What is the type of the 'data' in `model.find({}).exec(function(err,data){});` function? Is it an Array or something else? – Naveen Attri Jul 03 '16 at 16:43
  • It's an array of mongoose model instances. – Peter Lyons Jul 03 '16 at 17:40
  • I just spent entirely too long on this as well, I found the name mongoose.model('Link', linkSchema, 'links '); had a space at the end of it, which is not the same as it without the space. – tbh__ Feb 25 '17 at 18:36
  • 1
    I didn't really understand which part of this answer would fix my problem. After a while I realized, that the 3rd argument of mongoose.model() is a specified collection name. The first argument is the model's name, 2nd is the schema value, 3rd is the collection name. After I specified the 3rd value as my collection name, poof, the array was no longer empty – SeanMC Jul 21 '19 at 18:46
0

From official doc

Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s).

Try it without populate

Model.find({}).exec(function(err, models) {
            if (err) {
                res.render('error', {
                    status: 500
                });
            } else {
                res.json(models);
            }
        });
hawk
  • 5,409
  • 2
  • 23
  • 29