【发布时间】:2017-02-22 02:07:45
【问题描述】:
这是我的架构:
/** Schemas */
var profile = Schema({
EmailAddress: String,
FirstName: String,
LastName: String,
BusinessName: String
});
var convSchema = Schema({
name: String,
users: [{
type: Schema.Types.ObjectId,
ref: 'Profiles'
}],
conversationType: {
type: String,
enum: ['single', 'group'],
default: 'single'
},
created: {
type: Date,
default: Date.now
},
lastUpdated: {
type: Date,
default: Date.now
}
});
/** Models */
db.Profiles = mongoose.model('Profiles', profile);
db.Conversations = mongoose.model('ChatConversations', convSchema);
module.exports = db;
然后我尝试使用以下代码 (http://mongoosejs.com/docs/populate.html) 填充用户:
db.Conversations.find(query).populate('users').exec(function (err, records) {
console.log(records);
});
这将返回 records 但 users 数组作为空白数组 []。
我也尝试了相反的方法(http://mongoosejs.com/docs/api.html#model_Model.populate):
db.Conversations.find(query, function (err, records) {
db.Conversations.populate(records, {path: "users", select: "BusinessName"}, function (err, records) {
console.log(records);
});
});
结果是一样的。当我将参考资料检入个人资料收集记录时。
知道这里有什么问题吗?
【问题讨论】:
-
似乎一切正常。它为我工作。看不出有什么问题
-
哪一个有效?
标签: node.js mongodb mongoose mongoose-schema mongoose-populate