我有一个类似于以下结构的模式...
const itemSchema = new Schema({
sizes: [{
type: String,
enum: [/* some fixed sizes */],
}],
// other properties ...
});
我执行形如 Items.find({ sizes: { $elemMatch: 'someSize' } });
的查询操作。
因此,我想在元素中添加索引以便快速执行该查询。
应该像下面这样:
const itemSchema = new Schema({
sizes: [{
type: String,
enum: [/* some fixed sizes */],
index: true,
}],
// other properties ...
});
或者
const itemSchema = new Schema({
sizes: {
type: [{
type: String,
enum: [/* some fixed sizes */],
}],
index: true,
},
// other properties ...
});
或许还有第三种选择?