仅检索文档我的条件满足子数组中的所有元素

Retrieve document only my condition meet all element in sub array

如何仅检索满足子数组中所有元素的条件的文档?

我的文档:

    {
        "_id" : ObjectId("5234cc89687ea597eabee675"),
        "code" : "xyz",
        "tags" : [ 
            "school", 
            "book", 
            "bag", 
            "headphone", 
            "appliance"
        ],
        "qty" : [ 
            {
                "size" : "S",
                "num" : 10,
                "color" : "blue"
            }, 
            {
                "size" : "M",
                "num" : 45,
                "color" : "blue"
            }, 
            {
                "size" : "M",
                "num" : 60,
                "color" : "blue"
            }, 
            {
                "size" : "L",
                "num" : 100,
                "color" : "green"
            }
        ]
    }
    
    {
        "_id" : ObjectId("5234cc8a687ea597eabee676"),
        "code" : "abc",
        "tags" : [ 
            "appliance", 
            "school", 
            "book"
        ],
        "qty" : [ 
            {
                "size" : "6",
                "num" : 100,
                "color" : "green"
            }, 
            {
                "size" : "6",
                "num" : 50,
                "color" : "blue"
            }, 
            {
                "size" : "8",
                "num" : 100,
                "color" : "brown"
            }
        ]
    }

对于这个查询,我检索了这个文档,但我预计没有结果,因为在数组中有一个值为 60 的元素... 数组的所有元素必须满足条件

    db.getCollection('test').find({
                         qty: { $all: [
                                        { "$elemMatch" : { size: "M", num: { $lt: 50} } }
                                      ] }
                       } )

我的结果是:

    {
        "_id" : ObjectId("5234cc89687ea597eabee675"),
        "code" : "xyz",
        "tags" : [ 
            "school", 
            "book", 
            "bag", 
            "headphone", 
            "appliance"
        ],
        "qty" : [ 
            {
                "size" : "S",
                "num" : 10,
                "color" : "blue"
            }, 
            {
                "size" : "M",
                "num" : 45,
                "color" : "blue"
            }, 
            {
                "size" : "M",
                "num" : 60,
                "color" : "blue"
            }, 
            {
                "size" : "L",
                "num" : 100,
                "color" : "green"
            }
        ]
    }

或者在这种情况下只产生一个符合条件的项目,但不是全部:

    db.getCollection('test').aggregate([
        { $unwind: '$qty'},
        { $match: {'qty.size': {$eq: "M"}}},
        { $match: {'qty.num': {$lt: 50}}}
    ])

我的结果是:

    {
        "_id" : ObjectId("5234cc89687ea597eabee675"),
        "code" : "xyz",
        "tags" : [ 
            "school", 
            "book", 
            "bag", 
            "headphone", 
            "appliance"
        ],
        "qty" : {
            "size" : "M",
            "num" : 45,
            "color" : "blue"
        }
    }

您可以使用 $map 创建一个布尔值辅助数组来执行对您的条件的检查。然后,使用$allElementsTrue进行过滤。

db.collection.aggregate([
  {
    "$addFields": {
      "filterArr": {
        "$map": {
          "input": "$qty",
          "as": "q",
          "in": {
            $and: [
              {
                $eq: [
                  "$$q.size",
                  "M"
                ]
              },
              {
                $lt: [
                  "$$q.num",
                  50
                ]
              }
            ]
          }
        }
      }
    }
  },
  {
    "$match": {
      $expr: {
        "$allElementsTrue": "$filterArr"
      }
    }
  },
  {
    "$project": {
      filterArr: false
    }
  }
])

这里是Mongo playground供大家参考。