Search code examples
arraysdatabasemongodbfilterproject

MongoDb Filter Array


Data structure:

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

I try too like this:

IMG2

ERROR

/////////////////////////////////////////////////////////////// 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. ////////////////////////////////


Solution

  • You can do

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

    Working Mongo playground