2016-10-13 71 views
0

这里阵列的工作是我的架构:猫鼬填充不具有的ObjectID

/** 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); 
}); 

这回recordsusers阵列作为空白阵列[]

我也试过的其他方式(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); 
    }); 
}); 

结果相同。当我检查参考资料收集记录在那里。

任何想法这里有什么错?

+0

一切似乎都对。并为我工作。不能看到任何错误 – chirag

+0

哪一个工作? – Shreejibawa

回答

3

我把它通过重命名模型(第3 arguement)工作:

mongoose.model("Profiles", profile, "Profiles");

的问题是猫鼬在寻找profiles集合,但它的存在,在数据库Profiles。所以我将它重命名为Profiles以匹配确切的名称。

Phewww!感谢我。

0

我发现您已声明配置文件架构但引用配置文件。

ref: 'Profiles' 

var profile = Schema({ 
EmailAddress: String, 
FirstName: String, 
LastName: String, 
BusinessName: String 
}); 
+0

这两者之间没有关系。它只是一个变量。请检查我的答案了解更多详情。 – Shreejibawa