I am trying to find a good way to be able to sort on a collection that uses the Attribute Pattern. I have a collection of “products” where each product can have up to 500 attributes. Each customer can define custom fields they want to store for all of their products
I want to be able to write a query which returns all products for a single customer having price >= 100.
I am planning on having an indexes on:
{ "customerId": 1, "attr.k": 1, "attr.v": 1 }
{ "customerId": 1, "attr.v": 1, "attr.k": 1 }
My query to filter the records would be the following I think: { "cutomerId": 1, "attr": { $elemMatch: { "attr.k": "price", "attr.v": { $gte: 100 } } } }
My question is, how do I sort by the “price” using this pattern?
Example structure of “products” collection so far:
{
"productId": 1,
"customerId": 1,
"attr": [
{ "k": "price", "v": 42 },
{ "k": "color", "v": "red" }
]
}
Original post: How to sort custom fields using the Attribute Pattern