Is there a way to compare elements, by their index, within the same array in a MongoDB document? For example, consider the following entry:
{ "_id" : ObjectId("1"), "arr" : [ { "int" : 100 }, { "int" : 10 } ] }
I have a collection with numerous similar entries and I'm interested in querying the collection to identify those documents where the value of arr[0].int
is greater than arr[1].int
. It would also be helpful to determine the percentage difference between these two elements. For instance, in the given example,
index 1 is 10 times less than index 0
.
The query below successfully identifies elements in mongoDocs that are greater than a specified value:
db.collection.find( { "arr.0.int" : { $gt: 10 }})
I've experimented with different approaches but haven't found one that meets my requirements effectively, especially considering the large size of the dataset. Any insights on performance optimization would be greatly appreciated!
Thank you!