根据父级的文本过滤器查询文档

时间:2014-09-18 07:37:04

标签: performance mongodb schema

我的系统中有三个集合(A,B,C)。 A有一个B系列的参考,B系列有一个C系列的参考。 C集合有一个字段 - keywords,它是一个字符串数组。

A -> B -> C

问题在于,我想查询在各自的A集合中具有匹配关键字的所有C个文档。

我可以首先通过对$in字段应用keywords过滤器来查询C,然后获得B并最终获得所有A文档,但这对我来说似乎效率低下。有没有办法更快地完成这项工作?

更新:

完成示例 我有一个名为Campaign的集合,每个广告系列都有多个subscriptionsCampaign也可以有一组keywords(字符串数组)。我希望能够查询其广告系列与查询keywords匹配的所有订阅。

广告系列架构:

mongoose.model 'Campaign', new mongoose.Schema
    name:
        type: String
        required: true
    program:
        type : mongoose.Schema.ObjectId
        required: true
        ref: 'Program'
    days:
        type: Number
        required: true
    keywords:
        type: [String]
    commitment:
        type: Number
    performance:
        type: Number
    isEnabled:
        type: Boolean
        default: true

订阅架构

 SubscriptionSchema = new mongoose.Schema
        client:
            type: String
            required: true
        startDate:
            type: Date
            required: true
        campaign:
            type : mongoose.Schema.ObjectId
            required: true
            ref : 'Campaign'
        totalCredits:
            type : Number
            required: true
        usedCredits:
            type : Number
            required: true
            default: 0
        created:
            type: Date
            default: Date.now
        data:
            type: mongoose.Schema.Types.Mixed
    model = mongoose.model 'Subscription', SubscriptionSchema

更新2 另一个解决方案可能是 - 创建一个虚拟属性来查询其Campaign中的关键字,但只有在我可以对虚拟属性进行索引时才会有效。

0 个答案:

没有答案