繁体   English   中英

猫鼬查询不返回值

[英]Mongoose query not returning values

我有一个名为plotCasts的CosmosDB集合,它具有如下所示的对象:

{ ... "owner" : "winery", "grower" : "Bill Jones", ... }

我有以下猫鼬模式:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const plotCastSchema = new Schema({
    owner: String,
    grower: String,
   ...
});

const ModelClass = mongoose.model('plotCast', plotCastSchema);

module.exports = ModelClass;

但是,当我使用下面的查询查询数据库时,我得到一个空数组作为结果。 知道为什么吗?

PlotCast.find({ owner: 'winery' }).lean().exec(function(err, results) {
                if (err) {
                    res.send(err);
                } else if (!results) {
                    res.send(null);
                } else {
                    res.send(results);
                }
            });

好的,您为模型plotCast命名,但是您的集合是plotCasts。

您可以通过以下方式强制设置收藏名称:

const plotCastSchema = new Schema({
    owner: String,
    grower: String,
    ...
}, { collection: 'plotCasts' });

或者,只需使用集合名称作为第一个参数,在猫鼬中定义您的模型,方法是:

const ModelClass = mongoose.model('plotCasts', plotCastSchema);

请让我知道是不是:)

问题是命名数据库时总是将模式保存为复数形式,因此应如下所示

PlotCasts.find({ owner: 'winery' }).lean().exec(function(err, results) {
            if (err) {
                res.send(err);
            } else if (!results) {
                res.send(null);
            } else {
                res.send(results);
            }
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM