Shr*_*awa 2 mongoose mongodb node.js mongoose-populate mongoose-schema
这是我的架构:
/** 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;
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用以下代码(http://mongoosejs.com/docs/populate.html)填充用户:
db.Conversations.find(query).populate('users').exec(function (err, records) {
console.log(records);
});
Run Code Online (Sandbox Code Playgroud)
这是返回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);
});
});
Run Code Online (Sandbox Code Playgroud)
结果是一样的。当我将引用检查到个人资料集合记录时。
知道这里有什么问题吗?
我通过重命名模型(第三个论点)使其工作:
mongoose.model( "Profiles", profile, "Profiles" );
问题是猫鼬正在搜索profiles集合,但它Profiles在数据库中。所以我将它重命名Profiles为匹配确切的名称。
噗!谢谢我。
| 归档时间: |
|
| 查看次数: |
3122 次 |
| 最近记录: |