How to Check each element of array has same values

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).

db.getCollection('test').find(
  { "array.0" : {$in : ["abcd", "cdfg"], 
    "array.1" : {$in : ["abcd","cdfg"] 
  }
 )

I am not sure I really understand your issue but the following might work.

db.getCollection("test").find( { "array" : { $in : ["abcd", "cdfg"] } } )

By the way you are missing a couple of closing parenthesis original query. It should be

db.getCollection("test").find(
{ "array.0" : {$in : ["abcd", "cdfg"] },
"array.1" : {$in : ["abcd","cdfg"] }
}
)

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.

please ignore type issue …

Thanks for the reply.
thanks for the reply.

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.

{
  code: "0001",
  array:[ "1","2"]
}

{
  code: "0002",
 array: ["3","2"]
}

{
  code: "0003",
  array: ["1","2"]
}

Hi @Chiku,

You will probably need to use an $all expression:

{ "array" : { $all : ["abcd", "cdfg"] } } )

If you wish go get only array with only those elements you should use and aggregation and match only on documents with $size of array equals to true.

Best
Pavel

1 Like

$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.

Documents:-

{
    "_id" : ObjectId("5f9d53673fbd2c5684bb67b4"),
    "code" : 1.0,
    "array" : [ 
        1.0, 
        2.0
    ]
}

/* 2 */
{
    "_id" : ObjectId("5f9d537e3fbd2c5684bb67b5"),
    "code" : 2.0,
    "array" : [ 
        3.0, 
        2.0
    ]
}

/* 3 */
{
    "_id" : ObjectId("5f9d53973fbd2c5684bb67b6"),
    "code" : 3.0,
    "array" : [ 
        1.0, 
        2.0
    ]
}

/* 4 */
{
    "_id" : ObjectId("5f9d54423fbd2c5684bb67b7"),
    "code" : 4.0,
    "array" : [ 
        3.0, 
        3.0
    ]
}

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 .

This will work i.e. $in and $all:------

db.getCollection('test').find({"array.0":{$in :[1,2]},
                                "array.1": {$in:[1,2]}
                            }  
   )
db.getCollection('test').find({"array":{$all :[1,2]}
                            }  
   )

This will not work if i.e. $in and $all for 3 values will give different result:-

db.getCollection('test').find({"array.0":{$in :[1,2,3]},
                                "array.1": {$in:[1,2,3]}
                            }  
   )
                            
db.getCollection('test').find({"array":{$all :[1,2,3]}
                            }  
   )