什么是包含'ref'但不指定types的Mongoose模型属性?

对于mongoose来说很新奇 – 我正在做一个现有的项目,并且已经被赋予了改变一些模型属性的任务。 我明白,如果一个模型包含这种属性

postedBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' } 

该属性引用另一个模型/模式,并访问该链接的模型/模式需要populate它来访问该属性。

但是在我正在审查的代码中(我没有写),这个types有很多属性

 contentTypes: [{ ref: 'ContentType' }], source: { ref: 'Source',required: true }, 

在哪里引用另一个模式,但没有types。 这是同一种关系,并暗示了id ? 这是一个子文件吗?

作为一个额外的问题:如果在一个模型中,我想引用一个链接模型(或模式)的属性,我需要先populate ? 也就是说,如果它是一个子文档,我可以使用点符号,但如果它是一个“链接”的文档,我不知道。

答案是模型模式不是独立的,而是被传递给模型“工厂”,这就给了他们所需要的属性types。

因此,从该工厂下面的片段(下面)。 我查看了mongoose-autopopulate的文档,但是我没有看到autopopulate=true含义。

 new: function(name, properties, statics, methods, schemaMods) { // Add default definition to properties with references and load reference schemas Object.keys(properties).forEach(function(key) { var modifiedProperty = (property) => { if (property.ref) { property.autopopulate = true; property.type = mongoose.Schema.Types.ObjectId; } return property; }; if (Array.isArray(properties[key]) && properties[key].length === 1) { properties[key][0] = modifiedProperty(properties[key][0]); } else { properties[key] = modifiedProperty(properties[key]); } });