I am trying to do a range query on the ENTIRE embedded document. For example:
{tag: {$gte: {foo: MinKey()}, $lte: {foo: MaxKey()}}
// Values aren't always Min/MaxKey, but in this example, I'm trying to match any documents with the key "foo"
According to https://docs.mongodb.com/manual/reference/bson-type-comparison-order/#objects:
1. Recursively compare key-value pairs in the order that they appear within the BSON object.
2. Compare the key field names.
3. If the key field names are equal, compare the field values.
4. If the field values are equal, compare the next key/value pair (return to step 1). An object without further pairs is less than an object with further pairs.
As you can see, I have multiple tags (which I’m trying to treat as 2-tuples: key, value) on a patient. A tag key may or may not be present for a patient. Those that do exist, I would like to be sortable/range-queryable. Some example queries:
// Get patients with systolic blood pressure 150+.
{tags: {$elemMatch: {$gte: {bpSystolic: 150}, $lt: {bpSystolic: MaxKey()}}}}
// Get all patients that have a blood glucose reading.
{tags : {$elemMatch: {$gte: {bloodGlucose: MinKey()}, $lte: {bloodGlucose: MaxKey()}}}}
// Get all patients that have a blood glucose reading AND a systolic blood pressure reading.
{$and: [{tags : {$elemMatch: {$gte: {bloodGlucose: MinKey()}, $lte: {bloodGlucose: MaxKey()}}}}, {tags : {$elemMatch: {$gte: {bpSystolic: MinKey()}, $lte: {bpSystolic: MaxKey()}}}}]}
// Get patients with an alert.
{tags: {alert: true}}
// Sort patients by systolic blood pressure descending.
{tags : {$elemMatch: {$gte: {bpSystolic: MinKey()}, $lte: {bpSystolic: MaxKey()}}}} (sorted by {tags: -1})
A quick explanation for why I’m trying this route:
We have patients that need to be queried/ordered in a lot of different ways. I previously had created 30+ indexes to support those queries. I’m trying to merge these different data points into one indexed field, tags.