node.js 猫鼬模式创建
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10081611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mongoose schema creation
提问by Yaron Naveh
I've just started with mongoose. I have a creation script with mongoose that creates the schemas and db with sample data.
我刚刚开始使用猫鼬。我有一个使用 mongoose 的创建脚本,它使用示例数据创建模式和数据库。
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:
换句话说,我是否需要在每个使用 mongoose 访问数据库的应用程序中运行此代码,或者只是第一次运行:
var Comments = new Schema({
title : String
, body : String
, date : Date
});
How would the answer change if I have setters/validations/etc?
如果我有设置器/验证/等,答案会如何变化?
回答by freakish
One defines Schemaso application understands how to map data from the MongoDB into JavaScript objects. Schemais a part of application. It has nothingto 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 everyapplication that needs it. It also applies to getters/setters/validations/etc.
一个定义Schema所以应用程序了解如何将数据从 MongoDB 映射到 JavaScript 对象。Schema是应用程序的一部分。它与数据库无关。它只将数据库映射到 JavaScript 对象。所以是的 - 如果您想获得良好的映射,您需要在每个需要它的应用程序中运行此代码。它也适用于 getter/setter/validations/等。
Note however that doing this:
但是请注意,这样做:
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);
will register Schemaglobaly. This means that if the application you are running is using some exterior module, then in this module you can simply use
将注册全球Schema。这意味着如果您正在运行的应用程序正在使用某个外部模块,那么在该模块中您可以简单地使用
var mongoose = require('mongoose');
var Comments = mongoose.model("Comments");
Comments.find(function(err, comments) {
// some code here
});
(note that you actually need to register the Schemabefore using this code, otherwise an exception will be thrown).
(注意,Schema在使用这段代码之前,你实际上需要注册,否则会抛出异常)。
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.jsmay look like this
然而,所有这些都只在一个节点会话中有效,因此如果您正在运行另一个需要访问 的节点应用程序Schema,那么您需要调用注册码。所以在单独的文件中定义所有模式是个好主意,例如comments.js可能看起来像这样
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);
};
then create file models.jswhich may look like this
然后创建models.js看起来像这样的文件
var models = ['comments.js', 'someothermodel.js', ...];
exports.initialize = function() {
var l = models.length;
for (var i = 0; i < l; i++) {
require(models[i])();
}
};
Now calling require('models.js').initialize();will initialize all of your Schemas for a given node session.
现在调用require('models.js').initialize();将为给定节点会话初始化所有架构。
回答by fbeshears
You do need to run this initialization code every time you run your app to register your app's Schemas with mongoose.
每次运行应用程序以向 mongoose 注册应用程序的架构时,您都需要运行此初始化代码。
When your app ends, mongoose does not store your Schema(s). So, the next time you run an app that uses a Schema, you need to register your Schema(s) again.
当您的应用程序结束时,猫鼬不会存储您的架构。因此,下次运行使用架构的应用程序时,您需要再次注册您的架构。
However, it's fairly easy to set up your app to do so.
但是,设置您的应用程序相当容易。
Here are two links to code that demonstrates how one can initialize schemas in mongoose. The first is in JavaScript, the second is in CoffeeScript.
这里有两个代码链接,演示了如何在 mongoose 中初始化模式。第一个是 JavaScript,第二个是 CoffeeScript。
https://github.com/fbeshears/register_models
https://github.com/fbeshears/register_models
https://github.com/fbeshears/register_coffee_models
https://github.com/fbeshears/register_coffee_models
The JavaScript demos is just one app.
JavaScript 演示只是一个应用程序。
The CoffeeScript code has two separate apps. The first stores documents with MongoDB, the second finds and displays the documents stored by the first app.
CoffeeScript 代码有两个独立的应用程序。第一个使用 MongoDB 存储文档,第二个查找并显示第一个应用程序存储的文档。

