猫鼬模式类型和参考?它们是 .populate 方法的关键词吗?或者他们是任意的话?

Mongoose schemas type and ref? are they key words for .populate method? or are they arbitrary words?

我正在尝试使用引用在 Mongoose 中创建数据关联,我想将来自 commentSchema 的 ID 添加到我的 objectScehma。 我从某处复制了这段代码并且它有效。

我的问题是-- 的名称用于注释数组中的对象:类型 ref-- 这两个词是 .populate 方法使用的猫鼬中某种类型的关键词吗???或者我们可以给这两个键起什么名字吗?我已将我的代码复制粘贴到下方,如有任何帮助,我们将不胜感激。

谢谢!!!

var mongoose = require("mongoose");

var objectSchema = new mongoose.Schema({
        name: String,
        image: String,
        description: String,
        comments:[
                {
                    type: mongoose.Schema.Types.ObjectId,
                    ref: "comment"
                }
        ]
    });

module.exports = mongoose.model("campground", objectSchema)

type and ref-- are these two words sometype of keys words in mongoose, (...)

是的。在内部,mongoose 在其算法中使用这两个键来设置您的 mongoose 模型。您可以在此处查看 type 键的用法:https://github.com/Automattic/mongoose/blob/master/lib/schema.js#L358

您可以按照此处的说明自由覆盖它:http://mongoosejs.com/docs/guide.html#typeKey

至于ref,如果我没记错的话,mongoose内部保存了一个"cache"所有的模型定义,使用ref键快速获取其内部的模型定义列表。我在这方面可能是错的,所以不要半信半疑。