【问题标题】:is there any mongo db query to only fetch searching elements all the nested elements是否有任何 mongo db 查询只获取所有嵌套元素的搜索元素
【发布时间】:2023-01-20 20:56:27
【问题描述】:

我的数据结构是

[
  {
    "item": "journal",
    "qty": 25,
    "status": "A",
    "weekNumber": 1,
    "sortOrder": 1,
    "label": 1,
    "numberOfPossibleDays": 1,
    "editable": 1,
    "selectedDate": 1,
    "deliveryDays": 1,
    "products": [
      {
        "key": "item-one",
        "name": "item one",
        "tags": [
          "v",
          "b"
        ]
      },
      {
        "key": "item-two",
        "name": "item-two",
        "tags": [
          "a",
          "c",
          "d"
        ]
      },
      {
        "_id": 3,
        "name": "item-three",
        "tags": [
          "g"
        ]
      }
    ]
  },
  {
    "item": "notebook",
    "status": "b",
    "qty": 1,
    "weekNumber": 1,
    "sortOrder": 1,
    "label": 1,
    "numberOfPossibleDays": 1,
    "editable": 1,
    "selectedDate": 1,
    "deliveryDays": 1,
    "products": [
      {
        "key": "item-four",
        "name": "item four",
        "tags": [
          "a",
          "o"
        ]
      },
      {
        "key": "item-five",
        "name": "item-five",
        "tags": [
          "s",
          "a",
          "b"
        ]
      }
    ]
  }
]

我想找到所有带有标签“a”的元素,所以预期的响应应该是这样的

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "deliveryDays": 1,
    "editable": 1,
    "item": "journal",
    "label": 1,
    "numberOfPossibleDays": 1,
    "products": [
      {
        "key": "item-one",
        "name": "item one",
        "tags": [
          "v",
          "b"
        ]
      }
    ],
    "qty": 25,
    "selectedDate": 1,
    "sortOrder": 1,
    "status": "A",
    "weekNumber": 1
  },
  {
    "_id": ObjectId("5a934e000102030405000001"),
    "deliveryDays": 1,
    "editable": 1,
    "item": "notebook",
    "label": 1,
    "numberOfPossibleDays": 1,
    "products": [],
    "qty": 1,
    "selectedDate": 1,
    "sortOrder": 1,
    "status": "b",
    "weekNumber": 1
  }
]

我可以使用 $filter 运算符来过滤投影中产品数组的标签数组中包含“b”的元素。我认为这是非常冗长的代码。有什么方法可以让 mongoDB 发送所有值而不是像这样在查询中写入每个元素?

db.collection.find({
  "products.tags": "b"
},
{
  item: 1,
  qty: 1,
  "status": 1,
  "weekNumber": 1,
  "sortOrder": 1,
  "label": 1,
  "numberOfPossibleDays": 1,
  "editable": 1,
  "selectedDate": 1,
  "deliveryDays": 1,
  products: {
    $filter: {
      input: "$products",
      cond: {
        $in: [
          "v",
          "$$this.tags"
        ]
      }
    }
  }
})

【问题讨论】:

    标签: node.js mongodb mongodb-query


    【解决方案1】:

    你也许可以使用如下聚合:

    db.collection.aggregate([
      {
        $match: {
          "products.tags": "b"
        },
        
      },
      {
        $set: {
          products: {
            $filter: {
              input: "$products",
              cond: {
                $in: [
                  "v",
                  "$$this.tags"
                ]
              }
            }
          }
        }
      }
    ])
    

    它会给出与以前相同的结果,但您不必在每个字段中都写上field : 1 来保留它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-01
      • 2020-12-10
      • 1970-01-01
      • 2015-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多