MongoDb Filter Array

我是研究僧i 提交于 2021-01-27 19:30:10

问题


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


回答1:


You can do

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

Working Mongo playground




回答2:


If you just filter an element in an array, even if only one of the element matches, the whole array will be returned, and nothing will be returned if there was no match. Therefore, you need to separate your array into different documents using the $unwind operator, and only after it, try to filter the results using $match.

The following query might solve your problem:

db.collection.aggregate([
  {"$unwind": "$product"},
  {"$match": {"product.name": "FirstWarehouseName1"}} // replace here with the name you want
])

Working mongoplayground

If I may, just a tip: You should store each product in a different document, although MongoDB is schemaless and allow us to have almost any format of document, remember that each document should contain the equivalent to one row of information in a regular relational database. Otherwise, you'll start to have these problems, and additionally, performance issues.



来源:https://stackoverflow.com/questions/63150083/mongodb-filter-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!