我有一个猫鼬模型模式,我正在尝试为一些预先存在的mongodb文档构建。现有的mongo文件有自己对同一个集合的引用。我试图创建一个表示这种关系的模式,然后尝试使用populate method。该模式看起来像下面的(为简便起见去掉了一些特性):是否可以在mongoosejs模式中填充自引用?
var fileSchema = new Schema({
_id: { type: String },
pcap: {
files: [{ type: Schema.Types.ObjectId, ref: 'Files' }]
}
});
var file = artifactConn.model('Files', fileSchema, 'info');
我然后使用上述模型进行查询,然后填充像这样:
models.file.findById("91320684-9a1a-4f2a-a03f-63a7a208ec9b")
.populate('pcap.files') // have also tried just 'files' w/ the same result
.exec(function (err, file) {
if (!err) {
console.log(file);
} else {
console.log(err);
}
});
导致该
{ _id: '91320684-9a1a-4f2a-a03f-63a7a208ec9b',
pcap:
{ files: [] }
}
如果我在mongohub查看我的文档,它看起来像这样
{ "_id" : "91320684-9a1a-4f2a-a03f-63a7a208ec9b",
"pcap" : {
"files" : [
{ "_id" : "e4eed129-b4aa-46fc-8df2-3f3f92e6fe53" },
{ "_id" : "8c0ecb98-452e-475d-a521-feba5c3d1426" },
{ "_id" : "4b87c467-f396-4bcf-a7b0-8419e8441ec0" } ] } }
我在做我的电话填充错误吗?这是我有我的架构设置的方式吗?这是数据存储的方式吗?
它不再是一个空集,但现在是不是'文件:[不确定,不确定的,不确定]'。至少现在阅读参考文献,但没有填充它们。任何其他想法? – ahsteele
您还需要将填充的调用更新为:populate('pcap.files._id') – matthewtole
不幸的是'.populate('文件')','.populate('pcap.files._id')',有同样的影响,我仍然以'files:[undefined,undefined,undefined]'结尾。但是,'.populate('pcap.files')抛出一个错误'TypeError:无法读取未定义的属性'ref'。关于我失踪的任何想法? – ahsteele