mongodb - 如何创建具有本地字段和引用字段 mongoose 的复合索引?
问题描述
我正在使用 Mongoose 和 MongoDB。
我想根据架构中的字段和引用架构中的字段进行查询。这是我正在查询的模型模式。
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});
解决方案
尝试这个:
Model.index({'thirdPartyId': 1, 'parent_facility': 1})
推荐阅读
- android - How do i generate JWT from a secretKey which was generated using "PBKDF2withHmacSHA256" in android?
- json - Serialise object to JSON with array - preparing data for bar Flot Chart
- java - Struts2 Convention Plugin @Actions not mapping with Spring Boot
- javascript - 当使用javascript达到一定宽度时如何取消选中复选框?
- python - 使用具有特定集群的 sklearn 进行 K 折叠,而不是使用特定大小进行拆分
- swift - UITextField 在打开时被键盘部分隐藏
- c# - 接口标记为在外部库中实现,接口的属性不可见,为什么?
- html - 激活时如何使按钮更改颜色
- javascript - 如何在 Thunderbird Addon 中添加没有 XUL 的 onload 监听器?
- jquery - 使用 jquery html5lightbox 加载不一致