猫鼬模式创建 [英] mongoose schema creation
问题描述
我刚开始使用猫鼬.我有一个带猫鼬的创建脚本,用于创建模式和带有示例数据的数据库.
I've just started with mongoose. I have a creation script with mongoose that creates the schemas and db with sample data.
现在,我编写实际的应用程序.每次我的应用程序运行时,我是否需要创建架构对象?或者它已经以某种方式可用?
Now I write the actual application. Do I need to create the schema object each time my application runs, or is it already available somehow?
换句话说,我是否需要在每个使用猫鼬访问数据库的应用程序中或仅在第一次运行此代码?
In other words do I need to run this code in every app that uses mongoose to access the db or just the first time:
var Comments = new Schema({
title : String
, body : String
, date : Date
});
如果我有设置者/验证者/等等,答案将如何改变?
How would the answer change if I have setters/validations/etc?
推荐答案
一个定义了Schema
,因此应用程序了解如何将数据从MongoDB映射到JavaScript对象. Schema
是应用程序的一部分.它与数据库无关.它仅将数据库映射到JavaScript对象.所以是的-如果您想获得漂亮的映射,则需要在需要它的每个应用程序中运行此代码.它也适用于getters/setters/validations/etc.
One defines Schema
so application understands how to map data from the MongoDB into JavaScript objects. Schema
is a part of application. It has nothing to do with database. It only maps database into JavaScript objects. So yes - if you want to have nice mapping you need to run this code in every application that needs it. It also applies to getters/setters/validations/etc.
但是请注意,这样做:
var mongoose = require('mongoose');
var Schema = mongoose.Schema; // <-- EDIT: missing in the original post
var Comments = new Schema({
title : String
, body : String
, date : Date
});
mongoose.model("Comments", Comments);
将全局注册Schema
.这意味着,如果您正在运行的应用程序正在使用某个外部模块,那么在此模块中,您可以简单地使用
will register Schema
globaly. This means that if the application you are running is using some exterior module, then in this module you can simply use
var mongoose = require('mongoose');
var Comments = mongoose.model("Comments");
Comments.find(function(err, comments) {
// some code here
});
(请注意,在使用此代码之前,您实际上需要注册Schema
,否则将引发异常).
(note that you actually need to register the Schema
before using this code, otherwise an exception will be thrown).
但是,所有这些操作仅在一个节点会话中起作用,因此,如果您正在运行需要访问Schema
的另一个节点应用程序,则需要调用注册代码.因此,最好在单独的文件中定义所有架构,例如comments.js
可能看起来像这样
However all of this works only inside one node session, so if you are running another node app which needs the access to the Schema
, then you need to call the registration code. So it is a good idea to define all Schemas in separate files, for example comments.js
may look like this
var mongoose = require('mongoose');
var Schema = mongoose.Schema; // <-- EDIT: missing in the original post
module.exports = function() {
var Comments = new Schema({
title : String
, body : String
, date : Date
});
mongoose.model("Comments", Comments);
};
然后创建如下所示的文件models.js
then create file models.js
which may look like this
var models = ['comments.js', 'someothermodel.js', ...];
exports.initialize = function() {
var l = models.length;
for (var i = 0; i < l; i++) {
require(models[i])();
}
};
现在调用require('models.js').initialize();
将初始化给定节点会话的所有模式.
Now calling require('models.js').initialize();
will initialize all of your Schemas for a given node session.
这篇关于猫鼬模式创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!