Query an Array with Compound Filter Conditions on the Array Elements

Dear all,
the documenation (https://docs.mongodb.com/manual/tutorial/query-arrays/) states that

The following example queries for documents where the dim_cm array contains elements that in some combination satisfy the query conditions; e.g., one element can satisfy the greater than 15 condition and another element can satisfy the less than 20 condition, or a single element can satisfy both:

```
db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )

T tried the following: use('testdb');
db.testcol.drop();

db.testcol.insertMany([
  { '_id': 1, 'a': [0,6,8]},
  { '_id': 2, 'a': [1,6,8]},
  { '_id': 3, 'a': [6,8,1]},
  { '_id': 4, 'a': [6,8,0]},
]);

//This returns 2 documents:
 db.testcol.find  ( { a:{$eq: 6, $eq: 1  } } )

//This returns 4 documents:
db.testcol.find  ( { a:{$eq: 1, $eq: 6  } } )

My results seem to contradict what is stated in the docs....what do I miss here? Why are only 2 documents returned in the first find? The docs state *that in some combination satisfy the query conditions*
This should be the same for both, shouldn't it?
Thanks in advance, Markus

See https://www.mongodb.com/community/forums/t/request-for-explanation-chapter-4-query-operators-lecture/122914/2?u=steevej

Basically, the query

becomes

test> query = { a:{$eq: 6, $eq: 1  } }
{ a: { '$eq': 1 } }

and the query

becomes

test> query = { a:{$eq: 1, $eq: 6  } }
{ a: { '$eq': 6 } }

I was using mongosh and test> was the prompt.

Thanks, Steeve, I saw this with explain() as well…The point is that it contradicts what is shown in the docs that I referenced, doesn’t it?
This is what seems to be astonishing as the example shown there is basically the same as what I did…

I don’t think it does. Your example use $eq twice and one is lost because of JSON. All examples from the documentations do not repeat the same key within the same object.
db.test.

// Starting collection

> db.testcol.find()
{ _id: 1, a: [ 0, 6, 8 ] }
{ _id: 2, a: [ 1, 6, 8, 10 ] }
{ _id: 3, a: [ 6, 8, 1, 9 ] }
{ _id: 4, a: [ 6, 8, 0 ] }
{ _id: 5, a: [ 10 ] }

// Apply 2 conditions on the same array

> db.testcol.find( { a : { $gte : 10 , $size : 4 }})
{ _id: 2, a: [ 1, 6, 8, 10 ] }

// Note that _id:5 has an element $gte:10 but $size is not 4
// while _id:3 has $size:4 but no element $gte:10

Dear Steeve, you are right, as the explain output confirms:
“queryPlanner”: {

"plannerVersion": 1,

"namespace": "testdb.testcol",

"indexFilterSet": false,

"parsedQuery": {

  "a": {

    "$eq": 6

  }

},

it just omits the second condition. However, I do not understand why it does that. When I put something like
db.testcol.find ( { a:{$eq: 8, $eq: 6, } } ).explain()
it shows up as
“parsedQuery”: {

  "a": {

    "$eq": 6

  }

},

"

Changing the query to : db.testcol.find ( { a:{$lt: 2, $eq: 6, } } ).explain() leads to the expected

“queryPlanner”: {

"plannerVersion": 1,

"namespace": "testdb.testcol",

"indexFilterSet": false,

"parsedQuery": {

  "$and": [

    {

      "a": {

        "$eq": 6

      }

    },

    {

      "a": {

        "$lt": 1

      }

    }

  ]

},"

The link I provided gives the reason why. And it appears you did not follow that link as I do not see the count I usually see when a link is clicked. Basically, a query is JSON and only the last occurrence of duplicate keys is kept.

From mongosh:

object = { a : 1 , a : 2 , a : 4 , a : 5 }
{ a: 5 }