How to rewrite the following query without using array.position to return documents if $in values for each array element in the array match the given values. Please note values given are same for all array position element. This query does not pick up index on array so array.0+array.1 index is needed and if array has 10 element its not possible to write query as follows and also create index each element . Instead query should be rewritten in such a way that it return the same result as the following query and use index on array (multi key index).
Finally, it is best to enclose code with triple back ticks as the formatting is better and the quotes are not replaced by the fancy html matching quotes.
Thanks for the reply. The query you have suggested will not give the same result. It will return documents where array has any of the $in values provided but what I am looking for is return the documents only if all the elements of the array has same values. for example
For example you query will return all 3 documents if array: {$in : [1,2,]} but what i need is two documents where array.0 = [1,2] and array.1 = [1,2] . Please ignore syntax/{ etc.
$all check if all the values are same in the given array i.e. AND function which is not same as checking for $in which is OR function. To explain the issue I am attaching the sample documents and the original query which works but has limitation i.e. it need additional index on each array element and if number of elements increased (at this time two only) index size will increase and query will be complicated . Original query do not use index in array.
Now if you run following original query for two values and $all it will show you same result but if you check of 3 values it will not . I guess you have tested the solution with two values .