【问题标题】:Node js mongoose filter data from array in collectionNode js mongoose从集合中的数组中过滤数据
【发布时间】:2023-01-09 19:15:37
【问题描述】:

我有这样的回应 ..................................................... ..................................................... ..................................................... ...................................................

{
    "data": [
        {
            "user": "83k13bde05f40640j12075w",
            "products": [
                {
                    "type": "shoes",
                    "amount": 20
                },
                {
                    "type": "trousers",
                    "amount": 6
                }
            ],
            "inStock": false
        },
        {
            "user": "9dc3f7de05f40640j12075y",
            "products": [
                {
                    "type": "chairs",
                    "amount": 11
                },
                {
                    "type": "bags",
                    "amount": 16
                }
            ],
            "inStock": false
        },
        {
            "user": "6wb3f7ne35f40640m62p2gd",
            "products": [
                {
                    "type": "phones",
                    "amount": 2
                },
                {
                    "type": "clothes",
                    "amount": 15
                }
            ],
            "inStock": false
        }
    ]
}

这是输出上述响应的函数

exports.getProducts = async (req,res) => {
    const result = await Products
     .find({inStock: false})
     .select("-_id -createdAt -__v")
    .exec()
    if(!result) return res.status(400).json({ data: 'No product found' });
    if(result.err) return res.json({ err: err });
      
    return res.json({data: result});
}

但我只想获得数量大于的产品10

所以我的输出应该是这样的

{
    "data": [
        {
            "user": "83k13bde05f40640j12075w",
            "products": [
                {
                    "type": "shoes",
                    "amount": 20
                }
            ],
            "inStock": false
        },
        {
            "user": "9dc3f7de05f40640j12075y",
            "products": [
                {
                    "type": "chairs",
                    "amount": 11
                },
                {
                    "type": "bags",
                    "amount": 16
                }
            ],
            "inStock": false
        },
        {
            "user": "6wb3f7ne35f40640m62p2gd",
            "products": [
                {
                    "type": "clothes",
                    "amount": 15
                }
            ],
            "inStock": false
        }
    ]
}

我尝试使用

.find({'products.amount': { $gt: 10 }})

但它没有过滤掉响应

【问题讨论】:

    标签: node.js mongoose


    【解决方案1】:

    您尝试过 $elemMatch 运算符吗?

    const result = await Products
      .find({ 
        inStock: false, 
        products: { $elemMatch: { amount: { $gt: 10 } } } 
      })
      .select("-_id -createdAt -__v")
      .exec();
    

    【讨论】:

    • 是的,我试过了,它没有用,它仍然返回所有数量小于 10 的数据
    【解决方案2】:

    您可以使用聚合来实现此目的。 首先,您使用 $match 运算符查找数组中项目与您的条件匹配的文档。然后您可以使用 $project 和 $filter 运算符返回过滤后的数组。

    const result = await Products.aggregate([
    {
       "$match" : {
           "products" : {
              "$elemMatch" : { amount: { $gt: 10 } }
           },
       }
    },
    {
       $project: {
          user: 1,
          inStock: 1,
          products: {
            $filter: {
              input: "$products",
              as: "products",
              cond: { $gt: ["$products.amount", 10] }
            }
          }
       }
    }
    ])
    .select("-_id -createdAt -__v")
    .exec();
    

    进一步阅读:https://studio3t.com/knowledge-base/articles/filter-elements-from-mongodb-arrays/#how-to-use-filter-and-project

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-12
      • 2021-10-31
      • 2018-12-11
      • 1970-01-01
      • 2016-10-20
      • 1970-01-01
      相关资源
      最近更新 更多