仅检索我的状况在子数组中的所有元素

发布于 2025-01-22 19:18:01 字数 3308 浏览 0 评论 0原文

如何检索文件仅与子阵列中的所有元素相遇?

我的文档:

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

How to retrieve document only my condition meet all element in subarray?

my documents:

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

for this query I retreive this document but I expected no result, because in the array there is an element with the value 60...
all elements of the array must meet the condition

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

my result is:

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

Or in this situation only result a items that meet the condition, but not all :

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

my result is:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沩ん囻菔务 2025-01-29 19:18:01

您可以使用$ 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 供您参考。

You can use $map to create an auxiliary array of booleans to perform the checking on your criteria. Afterwards, use $allElementsTrue to perform the filtering.

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
    }
  }
])

Here is the Mongo playground for your reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文