Javascript 猫鼬填充未定义的字段
我看到很多关于这方面的问题,但我找不到问题所在。当我使用“填充”获取“外键”时,我的字段未定义 用户模型:Javascript 猫鼬填充未定义的字段,javascript,node.js,mongodb,mongoose,mongoose-populate,Javascript,Node.js,Mongodb,Mongoose,Mongoose Populate,我看到很多关于这方面的问题,但我找不到问题所在。当我使用“填充”获取“外键”时,我的字段未定义 用户模型: var userSchema = new Schema({ email : { type: String, required: true, unique: true }, password : { type: String, required: true }, firstname : { type: String, required: t
var userSchema = new Schema({
email : { type: String, required: true, unique: true },
password : { type: String, required: true },
firstname : { type: String, required: true },
lastname : { type: String, required: true },
created_at : Date,
updated_at : Date,
office : [ { type: Schema.Types.ObjectId, ref: 'Office' } ]
});
var User = mongoose.model('User', userSchema, 'User');
module.exports = User;
var officeSchema = new Schema({
name : { type: String, required: true },
address : String,
city : String,
geolocation : [ { type: Schema.Types.ObjectId, ref: 'Geolocation' } ],
company : [ { type: Schema.Types.ObjectId, ref: 'Company' } ]
});
var Office = mongoose.model('Office', officeSchema, 'Office');
module.exports = Office;
User.find({})
.populate('office')
//.populate('office', 'name') I tried this too
.exec(function (err, users) {
if (err) return handleError(err);
users.forEach(function(user){
console.log('Office name: ', user.office.name);
});
});
办公模式:
var userSchema = new Schema({
email : { type: String, required: true, unique: true },
password : { type: String, required: true },
firstname : { type: String, required: true },
lastname : { type: String, required: true },
created_at : Date,
updated_at : Date,
office : [ { type: Schema.Types.ObjectId, ref: 'Office' } ]
});
var User = mongoose.model('User', userSchema, 'User');
module.exports = User;
var officeSchema = new Schema({
name : { type: String, required: true },
address : String,
city : String,
geolocation : [ { type: Schema.Types.ObjectId, ref: 'Geolocation' } ],
company : [ { type: Schema.Types.ObjectId, ref: 'Company' } ]
});
var Office = mongoose.model('Office', officeSchema, 'Office');
module.exports = Office;
User.find({})
.populate('office')
//.populate('office', 'name') I tried this too
.exec(function (err, users) {
if (err) return handleError(err);
users.forEach(function(user){
console.log('Office name: ', user.office.name);
});
});
填充代码:
var userSchema = new Schema({
email : { type: String, required: true, unique: true },
password : { type: String, required: true },
firstname : { type: String, required: true },
lastname : { type: String, required: true },
created_at : Date,
updated_at : Date,
office : [ { type: Schema.Types.ObjectId, ref: 'Office' } ]
});
var User = mongoose.model('User', userSchema, 'User');
module.exports = User;
var officeSchema = new Schema({
name : { type: String, required: true },
address : String,
city : String,
geolocation : [ { type: Schema.Types.ObjectId, ref: 'Geolocation' } ],
company : [ { type: Schema.Types.ObjectId, ref: 'Company' } ]
});
var Office = mongoose.model('Office', officeSchema, 'Office');
module.exports = Office;
User.find({})
.populate('office')
//.populate('office', 'name') I tried this too
.exec(function (err, users) {
if (err) return handleError(err);
users.forEach(function(user){
console.log('Office name: ', user.office.name);
});
});
我想获取用户办公室名称。但是这个
user.office.name
返回我未定义,当我这样做user.office
时,我可以看到带有name字段的对象。但是我没有访问name字段的权限。用户模式中的office
字段被定义为数组。因此,要访问其元素,请使用user.office[0].name
,user.office[1].name
,等等
否则,请使用循环:
user.office
.forEach(function(each) {
console.log('Office name: ', each.name);
});
userSchema
中的office
字段定义为数组。因此,要访问其元素,请使用user.office[0].name
,user.office[1].name
,等等
否则,请使用循环:
user.office
.forEach(function(each) {
console.log('Office name: ', each.name);
});
您只需将查询编辑为
populate({path: "office", populate: {path:"company"}})
它还将填充公司数据。您只需编辑查询即可
populate({path: "office", populate: {path:"company"}})
它还将填充公司数据。
user.office
字段是架构中的一个数组。试试user.office[0]。name
等等。很好,你说得对!我可以将模型更改为使用不带数组的填充吗?它可以工作吗?您可以通过删除[
&]
来轻松地在架构级别更改它。而不是find()
使用findOne()
。它将在对象中返回一条记录。user.office
字段是架构中的一个数组。试试user.office[0]。name
等等。很好,你说得对!我可以将模型更改为使用不带数组的填充吗?它可以工作吗?您可以通过删除[
&]
来轻松地在架构级别更改它。而不是find()
使用findOne()
。它将在Object中返回一条记录。谢谢。我有一个关于你的问题。正如您在我的officeSchema中看到的,我有公司和地理位置对象ID。当我填充office时,可以在我的user find上同时填充这两个对象?是的,您可以。查看插件,或者你可以看看猫鼬是如何工作的。@Westen我不是这样的。我不在同一个模式中。用户架构不知道公司架构。所以我认为Mongoose Deep Populate插件是目前最好的解决方案谢谢。我有一个关于你的问题。正如您在我的officeSchema中看到的,我有公司和地理位置对象ID。当我填充office时,可以在我的user find上同时填充这两个对象?是的,您可以。查看插件,或者你可以看看猫鼬是如何工作的。@Westen我不是这样的。我不在同一个模式中。用户架构不知道公司架构。因此,我认为Mongoose Deep Populate插件是我能用user.office.company.name
抓到的最棒的解决方案,谢谢。但是你知道mongoose的填充性能是否真的比SQL关系好吗?@John当然mongo是文档数据库,mongo的性能比SQL关系好。如果你喜欢答案,请向上投票。哦,好的。我听说SQL更好地管理两个表之间的关系。因此,如果NoSQL足够好,我将继续使用mongoOh nice,我可以使用user.office.company.name
获取它,谢谢。但是你知道mongoose的填充性能是否真的比SQL关系好吗?@John当然mongo是文档数据库,mongo的性能比SQL关系好。如果你喜欢答案,请向上投票。哦,好的。我听说SQL更好地管理两个表之间的关系。因此,如果NoSQL足够好,我将继续使用mongo