正确的mongoose架构集合属性方法

时间:2016-07-21 07:41:21

标签: node.js mongoose

我需要阅读标识为连接到猫鼬model的集合。是正确的方法来直接读取属性model.schema.options.collection还是有更合适的方法?

1 个答案:

答案 0 :(得分:0)

据我所知,您希望使用mongoose架构的属性。我将为您提供一个如何有效使用它的小例子。

您的模型文件

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

//Schema for User
var UserSchema = new Schema({
    name: {
         type: String,
    },
    email: {
         type: String
    },
    password: {
         type: String,
    },
});

module.exports = mongoose.model('User', UserSchema);

功能中,您希望包含此模型

var User = require('/path/models/user');

var  UserController = {
     create: function(req, res){

           var user = new User();

           user.name = req.body.name;
           user.email = req.body.email;
           user.password = req.body.password;

           //rest of the functionality on these bod params.
     }
}

我希望这个小例子能够提供帮助。