【问题标题】:Mongoose populate ObjectID from multiple possible collectionsMongoose 从多个可能的集合中填充 ObjectID
【发布时间】:2017-03-30 00:18:55
【问题描述】:

我有一个看起来像这样的猫鼬模型

var LogSchema = new Schema({
    item: {
        type: ObjectId,
        ref: 'article',
        index:true,
    },
});

但“item”可以从多个集合中引用。有可能做这样的事情吗?

var LogSchema = new Schema({
    item: {
        type: ObjectId,
        ref: ['article','image'],
        index:true,
    },
});

“item”可以是“article”集合或“image”集合中的文档。

这是可能的还是我需要手动填充?

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    首先是一些基础知识

    ref 选项表示当您使用 populate() 时,猫鼬要为哪个集合获取数据。

    ref 选项不是强制性的,当你不设置它时,populate() 要求你使用model 选项动态地给他一个ref。 p>

    @example

     populate({ path: 'conversation', model: Conversation }).
    

    这里你对猫鼬说,ObjectId 后面的集合是Conversation

    不可能populateSchema 提供refs 的数组。

    其他一些Stackoverflow people 询问了这个问题。


    解决方案 1:填充两者(手动)

    尝试填充一个,如果没有数据,填充第二个。


    解决方案 2:更改架构

    创建两个链接,并设置其中一个。

    var LogSchema = new Schema({
        itemLink1: {
            type: ObjectId,
            ref: 'image',
            index: true,
        },
        itemLink2: {
            type: ObjectId,
            ref: 'article',
            index: true,
        },
    });
    
    
    LogSchema.find({})
         .populate('itemLink1')
         .populate('itemLink2')
         .exec()
    

    【讨论】:

    • 啊!谢谢,我不知道您可以将模型传递给 populate() 函数!这是一个巧妙的技巧,不幸的是,解决方案 1 是我在这个项目中的唯一选择。谢谢!
    【解决方案2】:

    问题很旧,但也许其他人仍在寻找类似的问题:)

    我在 Mongoose Github 中发现了这个问题:

    mongoose 4.x 支持使用 refPath 代替 ref:

    var schema = new Schema({
      name:String,
      others: [{ value: {type:mongoose.Types.ObjectId, refPath: 'others.kind' } }, kind: String }]
    })
    

    在@CadeEmbery 的情况下,它将是:

    var logSchema = new Schema({
      item: {type: mongoose.Types.ObjectId, refPath: 'kind' } },
      kind: String
    })
    

    但我还没试过……

    【讨论】:

    【解决方案3】:

    通过refPath动态引用

    Mongoose 还可以根据文档中属性的值从多个集合中填充。假设您正在构建一个用于存储 cmets 的模式。用户可以评论博客文章或产品。

      body: { type: String, required: true },
      on: {
        type: Schema.Types.ObjectId,
        required: true,
        // Instead of a hardcoded model name in `ref`, `refPath` means Mongoose
        // will look at the `onModel` property to find the right model.
        refPath: 'onModel'
      },
      onModel: {
        type: String,
        required: true,
        enum: ['BlogPost', 'Product']
      }
    });
    
    const Product = mongoose.model('Product', new Schema({ name: String }));
    const BlogPost = mongoose.model('BlogPost', new Schema({ title: String }));
    const Comment = mongoose.model('Comment', commentSchema);
    

    【讨论】:

      猜你喜欢
      • 2019-05-26
      • 2021-03-03
      • 1970-01-01
      • 2021-03-19
      • 2017-04-20
      • 1970-01-01
      • 2012-12-07
      • 2021-08-25
      • 1970-01-01
      相关资源
      最近更新 更多