使用数组中的值查询项目

时间:2019-01-17 20:33:06

标签: arrays mongodb nosql mongodb-query

假设我的数据库中有很多与此记录相似的记录

{
  "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不同的entries

现在,我有一个basenamecrcentries的列表,我想要为其查找记录。像这样:

{
  "basename": {
    "$in": [
      "2020bb",
      "alpham2p"
    ]
  },
  "entries": {
    "$in": [
      [
        { "$elemMatch": { "crc": "4f5e19bd" } },
        { "$elemMatch": { "crc": "d6314bf0" } },
        { "$elemMatch": { "crc": "47fddfee" } }
      ],
      [
        { "$elemMatch": { "crc": "c763e52a" } },
        { "$elemMatch": { "crc": "7a0b435c" } },
        { "$elemMatch": { "crc": "efc9ae2e" } }
      ]
    ]
  }
}

通过此查询,我试图找到具有以下条件的项目

  • 具有basename2020bb的{​​{1}} entriescrc
  • 4f5e19bd, d6314bf0, 47fddfee basename和带有alpham2pentries的{​​{1}}

我希望我的意图很明确。不幸的是,这只给了我0条结果。

1 个答案:

答案 0 :(得分:-1)

db.collection.find({
    basename: {
        "$in": ["2020bb", "alpham2p"]
    },
    entries: {
        $elemMatch: {
            crc: {
                $in: ["4f5e19bd", "d6314bf0", "47fddfee", "c763e52a", "7a0b435c", "efc9ae2e"]
            }
        }
    }
})