首页 > 解决方案 > 对象数组的索引参数

问题描述

我想对数组内对象的“文本”参数进行索引,因为我正在做一个搜索栏,并将查看模型的“标题”和“标签”字段

const needSchema = new Schema({
    owner: {
        type: ObjectId,
        required: true,
        ref: User
    },
    title: {
        type: String,
        required: true
    },
    tags: {
        type: [Object],
        required: false
    },
}, {
        timestamps: {
            createdAt: 'created_at',
            updatedAt: 'updated_at'
        }
    });
needSchema.index({ title: 'text', 'tags.text': 'text' });

const Need = mongoose.model('Need', needSchema);

实际上只有标题有效。我无法获得对象标签数组的结果。标签数组:

tag:[
   {'id':'text1', 'text':'text1',
   {....},
]

What is wrong?

标签: mongodbexpressmongooseschema

解决方案


推荐阅读