MongoError: cannot nest $ under $in

Viewed 4167

I am trying to query multiple documents that have a pair of basename and entries, while entries is an array containing a field called crc.

Example of 2 item2:

{
  "basename": "2020bb",
  "entries": [
    {
      "name": "030-c1.bin",
      "extension": "bin",
      "basename": "030-c1",
      "crc": "4f5e19bd",
      "size": 1048576
    },
    {
      "name": "030-c2.bin",
      "extension": "bin",
      "basename": "030-c2",
      "crc": "d6314bf0",
      "size": 1048576
    },
    {
      "name": "030-c3.bin",
      "extension": "bin",
      "basename": "030-c3",
      "crc": "47fddfee",
      "size": 1048576
    }
  ]
},
{
  "basename": "mutnat",
  "entries": [
    {
      "name": "sp-s2.sp1",
      "extension": "sp1",
      "basename": "sp-s2",
      "crc": "9036d879",
      "size": 1048576
    },
    {
      "name": "sp-s3.sp1",
      "extension": "sp1",
      "basename": "sp-s3",
      "crc": "c7f2fa45",
      "size": 1048576
    },
    {
      "name": "sp-4.sp1",
      "extension": "sp1",
      "basename": "sp-s4",
      "crc": "e72943de",
      "size": 1048576
    }
  ]
}

I know the basenames and crcs and I am trying to find many items containing proper pairs. For this I am using $in, $all and $elemMatch

{
  "basename": {
    "$in": [
      "2020bb",
      "mutnat"
    ]
  },
  "entries": {
    "$in": [
      {
        "$all": [
          { "$elemMatch": { "crc": "4f5e19bd" } },
          { "$elemMatch": { "crc": "d6314bf0" } },
          { "$elemMatch": { "crc": "47fddfee" } }
        ]
      },
      {
        "$all": [
          { "$elemMatch": { "crc": "9036d879" } },
          { "$elemMatch": { "crc": "c7f2fa45" } },
          { "$elemMatch": { "crc": "e72943de" } }
        ]
      }
    ]
  }
}

Mind that just for the brievety of this example I only have one item inside the $ins. In reality there are multiple items in these arrays.

With this query however I get this error:

MongoError: cannot nest $ under $in

edit: clarified my data and my query

1 Answers
db.xxx.find({
  "basename": "2020bb",
  "entries": {
     $elemMatch: {
       "crc": { $in: ["4f5e19bd", "d6314bf0", "47fddfee" ] }
     }
   }
})
Related