I have a question regarding Mongo indexes for a $or request on two ObjectID fields.
How can I make sure a search will first look at the first argument of the $or expression and only then if no match looks at the second or should we split it into two requests and add this logic in our code?
I tried to use a compound index with weights but it's only working for text search.
Here is what I've tried :
@index({ user_id: 1 }, { unique: true, partialFilterExpression: { user_id: { $exists: true } } })
@index({ device_id: 1 }, { partialFilterExpression: { device_id: { $exists: true } } })
@index(
{ user_id: 1, device_id: 1 },
{
weights: {
user_id: 10,
},
partialFilterExpression: { user_id: { $exists: true }, device_id: { $exists: true } },
},
)
@modelOptions({
schemaOptions: {
collection: 'test',
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at',
},
},
})
export class Test extends Base {
@prop({ required: false })
public user_id?: ObjectId
@prop({ required: false })
public device_id?: ObjectId
}
The request I'm trying :
db.test.find( { $or: [ { user_id: ObjectId('624c6bada5b7f846e80af8cb')}, { device_id: ObjectId('624c6bada5b7f846e80af8ca')}]} )
The indexes :
Thank you!

