2016-09-27 21 views
0

剛剛掌握MongoDB和Mongoose並在第一個障礙掙扎!不能讓貓鼬創建新的集合

我有一個名爲'Academy'的數據庫,並定義了一個名爲'Level'的模式。我的理解是,當我啓動我的Express服務器時,應該自動創建由Schema模型定義的集合,但由於某種原因,似乎沒有任何事情發生。當我轉到Mongo shell中的數據庫並鍵入show collections時,不顯示任何內容。

任何人都可以解釋我要去哪裏錯了嗎?我是否需要輸入數據以及定義模式 - 我曾希望暫時創建一個空集合並稍後填充數據。

謝謝!

級架構定義:

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

const LevelSchema = new Schema({ 
    level: Number, // e.g. 3 
    framework: String, // e.g. England/Scotland 
    sizes: [{ 
     name: String, // Award/Certificate/Diploma 
     credits: Number // e.g. 11, 24, 48 etc. 
    }], 
    delivery: [], // Workshop/Distance 
    venue: [], // Leeds/London/Edinburgh 
    courseRef: String, // 600/7760/8 
    units: [{ 
     number: String, // e.g. 01, 05 etc. 
     name: String, // UnitName 
     mandatory: [] // e.g. Award/Certificate/Diploma 
    }] 

}); 

const ModelClass = mongoose.model('Level', LevelSchema) 

module.exports = ModelClass; 

server.js - 這使用HTTP代理設置代理服務器使用的WebPack發展 - 如果你有興趣,請參閱this article獲取更多信息。我不認爲這是我遇到麻煩的原因,但是爲了以防萬一,我將它全部留在了那裏。

const express = require('express'); 
const path = require('path'); 
const httpProxy = require('http-proxy'); 
const mongoose = require('mongoose'); 


//DB SETUP 

const db = mongoose.connection; 
db.on('error', console.error); 
db.once('open', function(){ 
    const Levels = require('./data/models/Level'); 
}) 
mongoose.connect('mongodb://localhost/Academy'); 


// SERVER SETUP 
const proxy = httpProxy.createProxyServer(); 

const app = express(); 

var isProduction = process.env.NODE_ENV === 'production'; 
var port = isProduction ? process.env.PORT : 3000; 
var publicPath = path.resolve(__dirname, 'public'); 

app.use(express.static(publicPath)); 


if(!isProduction){ 
    var bundle = require('./server/bundle.js'); 
    bundle(); 
    app.all('/build/*', function(req, res){ 
     proxy.web(req,res, { 
      target: 'http://localhost:8080' 
     }); 
    }); 
} 

proxy.on('error', function(e){ 
    console.log('Could not connect to proxy, please try again...'); 
}) 

app.listen(port, function(){ 
    console.log('Server running on port ' + port); 
}); 

回答

1

我不認爲你做錯了什麼。只要你對Level模型採取行動(查找,插入等),應該自動創建集合。