Compound Indexing with id and another field of data class where id is also a field in my data class

Hello, hope you are doing fine.

I am using CompundIndex annotation on my data class where data calss have fields such as id, roomId and more . And my purpose is to use both id and roomId as compund index but unfortunately I am not able to do so and it is considering only id as index, can you please help me with that ?

@Document
@CompoundIndexes(value = [CompoundIndex(name = "operationId", def = "{'_id': 1, 'roomId': 1}", unique = true, sparse = true)])
data class Floor(
        override var id: ObjectId?,
        var roomId: ObjectId?,
        override val expiredAt: OffsetDateTime,
        val error: String? = null,
) : MongoExpiry

and let me know if more information is needed
Thanks,
Mukul

Hi @MUKUL_TYAGI

If the _id index offers a more efficient means of retrieving results, the query optimizer may select it over other indexes. Given that the _id field is unique, lookups based on it are typically very fast. If the roomId field has low cardinality—meaning it contains few distinct values—the optimizer might still favor the _id index when executing queries that filter by roomId over the compound index.

Ultimately, the optimizer will evaluate various factors to determine the most suitable index for the specific query pattern.