【发布时间】:2013-05-22 21:20:26
【问题描述】:
正在检查 Mongoose 和相对较新的填充方法。像这样从子级填充到父级时似乎工作完美:
var Comment = new Schema({
Message: { type: String }
User: { type: ObjectId, ref: 'User' }
});
var User = new Schema({
Username: { type: String }
Comments: [{ type: ObjectId, ref: 'Comment' }]
});
以下内容按预期工作。
Comment.find({}).populate({ path: 'User' }).exec(function (err, comments) {
if(err) handle(callback);
// no error do something with comments/user.
// this works fine and populates the user perfectly for each comment.
});
User.findOne({ Username: "some username"}).populate('Comments').exec(function (err, user) {
if(err) handle(callback);
// this throws no errors however the Comments array is null.
// if I call this without populate I can see the ref ObjectIds in the array.
});
ObjectIds 是可见的,无需在用户模型/模式上调用 populate,而且我可以从子端 ref 很好地填充这一事实,这使得配置看起来是正确的,但并不快乐。
上述架构被缩短,以免发布一英里长的代码列表(我讨厌那个!!!)。希望我错过了一些简单的东西。
【问题讨论】:
-
您是否尝试过将路径名取消大写?这不太可能是问题,但大写的路径名称不是惯用的,并且作为规范化过程的一部分,猫鼬可能在某处将它们小写。只是在这里抓住稻草,除此之外,你的例子是教科书。
-
至于教科书,我认为这是迟钝的。一定是一些愚蠢的东西。我喜欢对象/道具,当它们代表一种类型(.net 家伙,就像一个类)是上限时,只是为了一致性。想知道你是否是对的。我会在 ref 属性上尝试不同的情况。不过,我在这方面打我的头。
-
小写没有骰子...