2017-09-26 114 views
0

我有一个CosmosDB集合称为plotCasts,其中有看起来像这样的对象:猫鼬查询没有返回值

{ ... "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); 
       } 
      }); 
+0

酒庄包含数据库不存在这就是为什么它来空阵列 –

+0

@RaviTeja我不明白,你是什么意思?有一个情节播放的关键“所有者”具有“酿酒厂”的价值。 –

回答

2

好的,您将您的模型命名为plotCast,但您的收藏是plotCasts。

你可以强迫你的集合名称是这样的:

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

或者,在猫鼬与集合名称作为第一个参数简单地定义你的模型,这种方式:

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

请让我知道如果是这样的:)

+0

这有效!谢谢。那么在将来,模型的文件名应该与集合的文件名相同?这很奇怪,因为在我的用户架构中,我使用名称'user',而集合名为'users',并且它工作正常。 –

+1

这不是关于文件名,而是关于您传递给'mongoose.model('plotCast',plotCastSchema);'这里是'plotCast'的第一个参数,这是错误的,因为集合是'plotCasts',但是如果你可以在Schema中设置集合名称,正如我在答案中所显示的那样。 –

1

问题是命名分贝总是保存复数形式的架构,所以它应该像下面那样

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

解释它! –