關於nodejs連接多個mongodb表

這幾天真是蛋疼,爲了連接兩個表,每次都只有一個連上,另外一個連不上的情況!

錯誤代碼如下:

var mongoose = require('mongoose');

console.log("create model");
var Schema = mongoose.Schema;   //  創建模型
var mongooseSchema = new Schema({
    name: String,
    password: String,
    email: String,
    phone: String,
    address: String
}); //  定義了一個新的模型,但是此模式還未和users集合有關聯

console.log("model over");

var db = mongoose.createConnection('mongodb://localhost/chihuo');
var mongooseModel = db.model('users',mongooseSchema);

    var blogschema = new Schema({
        author:{type : String, defalut : 'nimingyonghu'},
        title: {type : String},
        data: {type : Date, default : Date.now},
        content: {type : String}
    });
    var blogModel = db.model('blogs',blogschema);

原來創建模型的時候,我沿用原先的模型,導致blog不生效!後面重新創建模型,搞定!代碼如下

var blogSchema = mongoose.Schema;

    var blogschema = new blogSchema({
        author:{type : String, defalut : 'nimingyonghu'},
        title: {type : String},
        data: {type : Date, default : Date.now},
        content: {type : String}
    });

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章