MongoDb过滤器阵列 [英] MongoDb Filter Array

查看:52
本文介绍了MongoDb过滤器阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据结构:

[{
  "_id": {
    "$oid": "5f1e91da1d840d673d159e69"
  },
  "product": [
    {
      "_id": "1",
      "name": "FirstWarehouseName1",
      "image": "FirstWarehousePhoto1",
      "manufacturer": "FirstWarehouseProducer1",
      "warehouse": "Warehouse1",
      "price": "32",
      "amount": 344
    },
    {
      "_id": "2",
      "name": "FirstWarehouseName2",
      "image": "FirstWarehousePhoto2",
      "manufacturer": "FirstWarehouseProducer2",
      "warehouse": "Warehouse1",
      "price": "12",
      "amount": 631
    },
    {
      "_id": "3",
      "name": "FirstWarehouseName3",
      "image": "FirstWarehousePhoto3",
      "manufacturer": "FirstWarehouseProducer3",
      "warehouse": "Warehouse1",
      "price": "66",
      "amount": 752
    }
  ],
  "_class": "pl.com.ttsw.intership.product_model.Products"
},{
  "_id": {
    "$oid": "5f1e91da1d840d673d159e6a"
  },
  "product": [
    {
      "_id": "1",
      "name": "SecondWarehouseName1",
      "image": "SecondWarehousePhoto1",
      "manufacturer": "SecondWarehouseProducerName1",
      "warehouse": "Warehouse2",
      "price": "32",
      "amount": 344
    },
    {
      "_id": "2",
      "name": "SecondWarehouseName2",
      "image": "SecondWarehousePhoto2",
      "manufacturer": "SecondWarehouseProducerName2",
      "warehouse": "Warehouse2",
      "price": "12",
      "amount": 631
    },
    {
      "_id": "3",
      "name": "SecondWarehouseName3",
      "image": "SecondWarehousePhoto3",
      "manufacturer": "SecondWarehouseProducerName3",
      "warehouse": "Warehouse2",
      "price": "66",
      "amount": 752
    }
  ],
  "_class": "pl.com.ttsw.intership.product_model.Products"
},{
  "_id": {
    "$oid": "5f1e91db1d840d673d159e6b"
  },
  "product": [
    {
      "_id": "1",
      "name": "ThirdWarehouseName1",
      "image": "ThirdWarehousePhoto1",
      "manufacturer": "ThirdWarehouse1",
      "warehouse": "Warehouse3",
      "price": "44",
      "amount": 123
    },
    {
      "_id": "2",
      "name": "ThirdWarehouseName2",
      "image": "ThirdWarehousePhoto2",
      "manufacturer": "ThirdWarehouse2",
      "warehouse": "Warehouse3",
      "price": "11",
      "amount": 442
    },
    {
      "_id": "3",
      "name": "ThirdWarehouseName3",
      "image": "ThirdWarehousePhoto3",
      "manufacturer": "ThirdWarehouse3",
      "warehouse": "Warehouse3",
      "price": "2",
      "amount": 2213
    }
  ],
  "_class": "pl.com.ttsw.intership.product_model.Products"
}]

IMG

我也这样尝试:

IMG2

错误

/////////////////////////////////////////////////////////////////我需要按名称来命名数组中的过滤器数组(仓库中的产品);如果我不做任何项目,则Array的所有产品都是如此.如果(按名称3"搜索)输出结果

/////////////////////////////////////////////////////////////// I need filter array in array(products in warehouses) by their names; If I do without project response is Array with all products. Output results if (search By "Name3")

[{
  "_id": {
    "$oid": "5f1e91da1d840d673d159e69"
  },
  "product": [
    {
      "_id": "3",
      "name": "FirstWarehouseName3",
      "image": "FirstWarehousePhoto3",
      "manufacturer": "FirstWarehouseProducer3",
      "warehouse": "Warehouse1",
      "price": "66",
      "amount": 752
    }
  ]
},{
  "_id": {
    "$oid": "5f1e91da1d840d673d159e6a"
  },
  "product": [
    {
      "_id": "3",
      "name": "SecondWarehouseName3",
      "image": "SecondWarehousePhoto3",
      "manufacturer": "SecondWarehouseProducerName3",
      "warehouse": "Warehouse2",
      "price": "66",
      "amount": 752
    }
  ]
},{
  "_id": {
    "$oid": "5f1e91db1d840d673d159e6b"
  },
  "product": [
    {
      "_id": "3",
      "name": "ThirdWarehouseName3",
      "image": "ThirdWarehousePhoto3",
      "manufacturer": "ThirdWarehouse3",
      "warehouse": "Warehouse3",
      "price": "2",
      "amount": 2213
    }
  ]
}]

我尝试了所有方法,但无法解决.//////////////////////////////

I tried all ways and I can't manage. ////////////////////////////////

推荐答案

您可以

[{
    $unwind: {
        path: "$product",
        preserveNullAndEmptyArrays: false
    }
}, {
    $match: {
        "product.name": {
            $regex: "Name3"
        }
    }
}, {
    $group: {
        _id: "$_id",
        product: {
            $push: "$product"
        }
    }
}]

工作蒙戈游乐场

这篇关于MongoDb过滤器阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆