I use Azure CosmosDb with MongoDb Api and got collection of documents with a structure bellow. I have to filter documents by parameter, ex.
x => x.Parameters.Any(xx => xx.Key == ParameterNames.ShiftId && (int)xx.Value == shiftId)
It seems to me to better performance I need to create index, but I cannot find any information how can I do it.
{
"_id": "08d8c696-2b7b-f227-d5dd-0647a8d51c1c",
"State": 2,
"Created": {
"$date": "2021-02-01T09:45:54.986Z"
},
"TailId": "e8fb236e-4d48-417b-bf5a-73f1d48fe239",
"Parameters": [{
"k": "ShiftId",
"v": 181
}, {
"k": "Id",
"v": "147814878155"
}, ....
]
}
For the Cosmos DB Mongo API, you could try adding a wildcard index on Parameters as described in the docs.
If you are in a position to change your model, you'll likely have better performance by refactoring the Parameters array into properties closer to the document root. For example:
{
"_id": "08d8c696-2b7b-f227-d5dd-0647a8d51c1c",
"State": 2,
...
"ShiftId": 181,
}
or
{
"_id": "08d8c696-2b7b-f227-d5dd-0647a8d51c1c",
"State": 2,
...
Parameters: {
"ShiftId": 181,
"Id": "147814878155"
}
}