node.js Model.find() 在猫鼬中返回空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18628656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:30:49  来源:igfitidea点击:

Model.find() returns empty in mongoose

node.jsmongodbexpressmongoose

提问by codeofnode

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

我正在使用 mongoose 列出 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
});

回答by Peter Lyons

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:

你的问题是猫鼬将集合复数化。Mongoose 正在查询“组织”,但您的数据在 mongodb 中作为“组织”。让它们匹配,你应该很高兴去。您可以通过 mongo shell 在 mongodb 中重命名它,也可以告诉 mongoose。从猫鼬文档:

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

// or

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

// or

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

回答by hawk

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);
            }
        });

回答by Levon

I will make Peter Lyonsanswer more simply , only add in mongodb collection name (s letter) . For example if you create collection which you want to call user or password, just write sletter in last of word (users,passwords) .

我会让Peter Lyons回答更简单,只添加 mongodb 集合名称(字母)。例如,如果您创建要调用user 或 password 的集合,只需在单词的最后一个写 s字母(用户密码)。