MongoDB :查找子文档的 none 符合条件的文档

MongoDB : Find document where none of the subdocument matches criteria

我想找出 Record A"FLD1""FLD1" 不等于 "VAL2" 的文档,这意味着它也应该 return 文档其中 Record A 不存在或 FLD1 不存在,除了 FLD1 不等于 VAL2.

在下面的文档中,应该return文档1, 34

只有 2 个 Record A 存在,FLD1 存在并且 FLD1 的值等于 VAL2.

{
    _id:NumberLong("1"),
    Name:"Doc1",
    Records: [ 
        {
            "RecordName" : "Record A",
            "State" : 1,
            "Properties" : {
                "FLD1" : "VAL1"
            }
        },
        {
            "RecordName" : "Record B",
            "State" : 1,
            "Properties" : {
                "FLD2" : "VAL3",
                "FLD3" : "VAL2"
            }
        }
    ]
},
{
    _id:NumberLong("2"),
    Name:"Doc2",
    Records: [ 
        {
            "RecordName" : "Record A",
            "State" : 1,
            "Properties" : {
                "FLD1" : "VAL2"
                "FLD4" : "VAL1"
                "FLD5" : "VAL6"
            }
        },
        {
            "RecordName" : "Record C",
            "State" : 1,
            "Properties" : {
                "FLD1" : "VAL1",
                "FLD2" : "VAL3"
            }
        }
    ]
},
{
    _id:NumberLong("3"),
    Name:"Doc3",
    Records: [ 
        {
            "RecordName" : "Record B",
            "State" : 0,
            "Properties" : {
                "FLD2" : "VAL2"
                "FLD3" : "VAL4"
                "FLD4" : "VAL5"
            }
        },
        {
            "RecordName" : "Record C",
            "State" : 1,
            "Properties" : {
                "FLD3" : "VAL2",
                "FLD5" : "VAL4"
            }
        }
    ]
},
{
    _id:NumberLong("4"),
    Name:"Doc4",
    Records: [ 
        {
            "RecordName" : "Record A",
            "State" : 1,
            "Properties" : {
                "FLD2" : "VAL2"
            }
        },
        {
            "RecordName" : "Record C",
            "State" : 1,
            "Properties" : {
                "FLD3" : "VAL3",
                "FLD4" : "VAL4"
            }
        }
    ]
}

有人知道如何为 mongo 数据库编写这样的查询吗?

此外,如果我想找出子文档的 none 中记录名称值为 Record B 的所有文档,我将如何找到?

在这种情况下,查询应该 return 记录 2 和 4。

您似乎需要 $or 包含三个单独过滤条件的运算符:

db.collection.find({
    $or: [
        { "Records": { $not : { $elemMatch: { "RecordName": "Record A" } } } },
        { "Records.Properties.FLD1": { $exists: false } },
        { "Records": { $not : { $elemMatch: { "Properties.FLD1": "VAL2" } } } }
    ]
})

$elemMatch along with $not allows you to check if all Records have RecordName not equal to Record A, then you can use $exists$not 检查 FLD1 和另一个 $elemMatch:

Mongo Playground

感谢@mickl

我不得不稍微修改一下答案。

{
      $nor: [
        {
          $and: [
            {
              "Records": {
                $elemMatch: {
                  "RecordName": "Record A"
                }
              }
            },
            {
              "Records.Properties.FLD1": {
                $exists: true
              }
            },
            {
              "Records": {
                $elemMatch: {
                  "Properties.FLD1": "VAL2"
                }
              }
            }
          ]
        }
      ]
    }