[英]How can Mongoose Populate not working with Array of ObjectIds?
[英]Mongoose Populate not working with Array of ObjectIds
這是我的架構:
/** 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);
});
});
結果是一樣的。 當我將引用檢查到個人資料集合記錄時。
知道這里有什么問題嗎?
我通過重命名模型(第三個觀點)使它起作用:
mongoose.model( "Profiles", profile, "Profiles" );
問題是Mongoose正在搜索profiles
集合,但在數據庫中作為Profiles
存在。 因此,我將其重命名為“ Profiles
以匹配確切名稱。
Phewww! 謝謝我
我發現您已經聲明了概要文件架構,但是引用了概要文件。
ref: 'Profiles'
var profile = Schema({
EmailAddress: String,
FirstName: String,
LastName: String,
BusinessName: String
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.