代码之家  ›  专栏  ›  技术社区  ›  Lukas

MongoError:不能在$下嵌套$

  •  1
  • Lukas  · 技术社区  · 5 年前

    我试图查询多个文档 basename entries ,同时 一个数组包含一个名为 crc

    {
      "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
        }
      ]
    }
    

    基名 s和 s和我正在尝试找到许多包含正确对的项目。为此,我使用 $in , $all $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" } }
            ]
          }
        ]
      }
    }
    

    当心 只是为了这个例子的可靠性 s、 实际上,这些数组中有多个项。

    MongoError: cannot nest $ under $in

    编辑:澄清我的数据和查询

    1 回复  |  直到 5 年前
        1
  •  0
  •   Paul Paulincai    5 年前
    db.xxx.find({
      "basename": "2020bb",
      "entries": {
         $elemMatch: {
           "crc": { $in: ["4f5e19bd", "d6314bf0", "47fddfee" ] }
         }
       }
    })