2017-09-26 153 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

解釋它! –