MongoDB和mongoose架构

时间:2014-01-09 10:22:42

标签: mongodb mongoose schema

这是models.js中的模型

var PatientSchema = new mongoose.Schema({
     _id : String,
     LastName : String,
     MiddleName : String,
     PatientIntId : String,
     Sex : String,
     Address1 : String,
     City : String,
     State : String,
     ZipCode : String,
     AccountNumber : String,
     Ssn : String
 });

var PatientInfoMdl = mongoose.model('PatientInfo',PatientSchema);
exports.PatientInfoMdl = PatientInfoMdl;

我访问数据的代码是:

var dbObj = require('../dataBase');
var config = require('../config');<
var moment = require('moment');
var models = require('../models/models');
var orm = require('orm');
var xml2js = require('xml2js');
var fs = require('fs');
var user = models.user;
var PatientInfoMdl = models.PatientInfoMdl;
exports.DisplayUsers = function (req, res) {
    var name = '';
    dbObj.connect(config.get('mongo'), function () {
        PatientInfoMdl.find()({}, function (err, docs) {
            if (err) 
                res.json(err);
            else res.render('index', { Patients : docs }); 
        });
    });
}

我没有得到数据,我的错误是什么?

2 个答案:

答案 0 :(得分:0)

我的错误是不遵循mongoDB中集合的命名约定。

Is there a convention to name collection in MongoDB?

答案 1 :(得分:0)

例如:

<强> Controller.js

var mongoose = require('mongoose');


var User = mongoose.model('User');

module.exports = {

  show: function(req, res) {

     User.find({}, function(err, users) {

      res.render('main', {users: users});

    })
  }
}

<强>型号:user.js的

// require mongoose
var mongoose = require('mongoose');

// create the UserSchema

var UserSchema = new mongoose.Schema({
  name: String
})

// register the schema as a model

var User = mongoose.model('User', UserSchema);

module.exports = {User}

<强> routes.js

// here we load the Quote model that we created on the server.js page

var mongoose = require('mongoose');

var User = mongoose.model('User');

// import users

var users = require('../controllers/users.js');

module.exports = function(app) {

  app.get('/', function(req, res) {

    res.send("Hello");

  })

  app.get('/user',function(req,res){

      users.show(req,res);

  })
}