Mongodb Mongoose和ExpressJS-模块化架构?
我想要每个型号的每个文件-即用户和国家/地区。 目前,我通过以下方式解决了这个问题: person.js country.js 所以我的问题如下:猫鼬是如何引用两个表的?例如,在Mongodb Mongoose和ExpressJS-模块化架构?,mongodb,mongoose,Mongodb,Mongoose,我想要每个型号的每个文件-即用户和国家/地区。 目前,我通过以下方式解决了这个问题: person.js country.js 所以我的问题如下:猫鼬是如何引用两个表的?例如,在Person模式中,我通过提供ref:'country'来引用国家,但我从未将模型包含在Person.js文件中。我做错什么了吗 更新: 考虑到以下答案,我现在有3个文件: database.js person.js country.js 现在,当我将所有这3个文件包括到一个测试文件中时,例如: var mongoose
Person
模式中,我通过提供ref:'country'
来引用国家,但我从未将模型包含在Person.js
文件中。我做错什么了吗
更新:
考虑到以下答案,我现在有3个文件:
database.js
person.js
country.js
现在,当我将所有这3个文件包括到一个测试文件中时,例如:
var mongoose = require('./database'),
User = mongoose.model('Person'),
Country = mongoose.model('Country');
我得到以下错误:丢失模式错误:模式尚未为模型“Person”注册。
。如果删除用户
变量,则国家/地区
会出现相同的错误。似乎mongoose正在尝试定义模型,而不是检索模型。有什么问题吗
更新2:
好的,当我现在包括这些文档时,我会执行以下操作:
require('./person');
require('./country');
var User = require('mongoose').model('Person'),
Country = require('mongoose').model('Country');
Mongoose为您注册的所有模型保留一个“注册表”。例如,
mongoose.model(“Country”)的“later”调用将检索命名模型。因此,实际上您不需要module.exports=Country`行,只需使用mongoose.model(“Country”)
调用在使用它的另一个模块中导入Country
。它是一个“全局”注册表,就像另一个模块中的require('mongoose')
不会创建mongoose
的另一个“实例”,而只是重新使用现有实例一样。有趣的概念。我已经更新了我的代码,但这种方法出现了一个错误。您“仍然”需要首先“定义”模型的模块,这是因为那里的列表使用mongoose“注册”了模型和附加的模式。我唯一告诉过你的是模块。导出不是必需的,因为mongoose会“按名称”调用模型。这才是真正回答你问题的关键。模型通过“名称”向mongoose“注册”,这当然是ref
的论点。你说得对,我没有仔细阅读你的回答。我已经在我的问题中添加了更新2:小节,这是你们的想法吗?或多或少。我只需要使用(除了模型定义所需的模块之外)var mongoose=require('mongoose')、User=mongoose.model(“Person”)
所以要理解,一旦您执行了mongoose.model('Person',personSchema)
之后,后续调用将“检索”而不是“注册”,因为初始调用会进行注册。一旦注册为'Person
,如果您使用小写字母ref:'Person'
,那么这实际上与注册的模型名称不匹配,并且当您尝试使用该引用执行任何操作时,例如.populate()
。Mongoose会保留您注册的所有模型的“注册表”。例如,mongoose.model(“Country”)的“later”调用将检索命名模型。因此,实际上您不需要module.exports=Country`行,只需使用mongoose.model(“Country”)
调用在使用它的另一个模块中导入Country
。它是一个“全局”注册表,就像另一个模块中的require('mongoose')
不会创建mongoose
的另一个“实例”,而只是重新使用现有实例一样。有趣的概念。我已经更新了我的代码,但这种方法出现了一个错误。您“仍然”需要首先“定义”模型的模块,这是因为那里的列表使用mongoose“注册”了模型和附加的模式。我唯一告诉过你的是模块。导出不是必需的,因为mongoose会“按名称”调用模型。这才是真正回答你问题的关键。模型通过“名称”向mongoose“注册”,这当然是ref
的论点。你说得对,我没有仔细阅读你的回答。我已经在我的问题中添加了更新2:小节,这是你们的想法吗?或多或少。我只需要使用(除了模型定义所需的模块之外)var mongoose=require('mongoose')、User=mongoose.model(“Person”)
所以要理解,一旦您执行了mongoose.model('Person',personSchema)
之后,后续调用将“检索”而不是“注册”,因为初始调用会进行注册。一旦注册为'Person
,如果您使用小写的ref:'Person'
,那么这实际上与注册的型号名称不匹配,并且当您尝试使用该引用执行任何操作时,如使用.populate()
,您将收到一个错误。
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/people');
mongoose.set('debug', true);
module.exports = mongoose;
/*jslint node: true */
'use strict';
var mongoose = require('./database'),
Schema = mongoose.Schema;
var userSchema = new Schema({
name: String,
username: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
admin: Boolean,
location: String,
meta: {
age: Number,
website: String
},
created_at: Date,
updated_at: Date,
country: {
type: Schema.Types.ObjectId,
ref: 'Country'
}
});
userSchema.methods.toString = function() {
return `I am ${this.username} and I'm from ${this.country.name}`;
}
mongoose.model('Person', userSchema);
/*jslint node: true */
'use strict';
var mongoose = require('./mongoose'),
Schema = mongoose.Schema;
var countrySchema = new Schema({
name: {
type: String,
required: true,
unique: true
},
created_at: Date,
updated_at: Date,
people: [{
type: Schema.Types.ObjectId,
ref: 'Person'
}]
});
mongoose.model('Country', countrySchema);
var mongoose = require('./database'),
User = mongoose.model('Person'),
Country = mongoose.model('Country');
require('./person');
require('./country');
var User = require('mongoose').model('Person'),
Country = require('mongoose').model('Country');