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 “jobs” where each customer can have up to 500,000 jobs. Each customer can define custom fields they want to store for all of their jobs which I’ve stored using the attribute pattern (example shown below).
I want to be able to write a query which returns all jobs for a single customer where their favorite number is greater than or equal to 100, sorted by their favorite number.
I am planning on having an index on { "customerId": 1, "attr.k": 1, "attr.v": 1 }
My query to filter the records would be the following I think: { "cutomerId": 1, "attr": { $elemMatch: { "attr.k": "favoriteNumber", "attr.v": { $gte: 100 } } } }
My question is, how do I sort by the “favorite number” using this pattern? Is there a better representation of the data that would allow me to have custom fields per customer and sort/filter based on those custom fields?
Example structure of “jobs” collection so far:
{
"jobId": 1,
"customerId": 1,
"attr": [
{ "k": "favoriteNumber", "v": 42 },
{ "k": "favoriteColor", "v": "green" }
]
}
[EDIT] - My initial idea - One idea I had to solve this was to store the attributes twice on the document like below, which would let me filter efficiently (I think) using the attribute pattern, and sort using the dictionary representation. I am looking to see if there is a better (or at least alternatives) because this approach stores duplicate data for each document which could add up significantly. Also, I’m not sure this sort would be able to use an index?
{
"jobId": 1,
"customerId": 1,
"attr": [
{ "k": "favoriteNumber", "v": 42 },
{ "k": "favoriteColor", "v": "green" }
],
"attrSort": {
"favoriteNumber": 42,
"favoriteColor": "green"
}
}