如何使用本地字段和参考字段猫鼬创建复合索引?

时间:2020-01-27 04:25:45

标签: mongodb mongoose mongoose-schema

我在MongoDB中使用Mongoose。

我想基于架构中的字段和引用架构中的字段进行查询。这是我正在查询的模型架构。

let schema = mongoose.Schema({
    parent_facility : { type: mongoose.Schema.Types.ObjectId, ref: 'Facility', required: true },
    thirdPartyId : mongoose.Schema.Types.Mixed
});

这是我想做的查询:

const modelsFound = await Model.find({thirdPartyId: 'randomidhere', parent_facility.status: 'complete'}).exec();

如何为此建立索引?

编辑:

这似乎可以按照我想要的方式工作

schema.createIndex({thirdPartyId: 1, 'parent_facility.status': 1});

1 个答案:

答案 0 :(得分:0)

尝试一下:

Model.index({'thirdPartyId': 1, 'parent_facility': 1})