Home > Software design >  Mongo find doesn't work for arrays field if it contained of part of object
Mongo find doesn't work for arrays field if it contained of part of object

Time:07-04

hope you are doing great guys! on the link below I created a collection and query but it doesn't work.

I know if we remove the filters property and leave the categories property on the body, the request will work well.

what is your suggestion for building a query by passing the same data as the body to get a correct result from MongoDB?

notice: target is finding the exact match records on DB, not any products which have any one of the filter objects on the filters array.

Appreciated...

https://mongoplayground.net/p/KIgVp57lTMb

CodePudding user response:

Option 1: Maybe something like this using aggregation/$filter:

db.collection.aggregate([
 {
  $match: {
    categories: "62445c4d922d127512867246"
   }
 },
{
"$addFields": {
  "filters": {
    "$filter": {
      "input": "$filters",
      "as": "f",
      "cond": {
        $or: [
          {
            "$and": [
              {
                "$eq": [
                  "$$f.value",
                  "626430bb19636d7db1804b78"
                ]
              },
              {
                "$eq": [
                  "$$f.name",
                  "Valve Type "
                ]
              }
            ]
          },
          {
            "$and": [
              {
                "$eq": [
                  "$$f.value",
                  "6264328519636d7db1804b86"
                ]
              },
              {
                "$eq": [
                  "$$f.name",
                  "Material Type"
                ]
              }
            ]
          },
          {
            "$and": [
              {
                "$eq": [
                  "$$f.value",
                  "6264363319636d7db1804c11"
                ]
              },
              {
                "$eq": [
                  "$$f.name",
                  "Ball / Trim Material"
                ]
              }
            ]
          }
        ]
       }
     }
   }
  }
 }
])

Explained: In find/match if you search documents containing at least single object you need to use $elemMatch , or you need to provide all elements of the object in the query , but you can do the search/project easier via aggregation $filter option as shown above.

Playground

Option 2: find/$elemMatch

Or if you want to find only the documents matching the criteria , without filtering the matching objects , here is the option:

db.collection.find({
 categories: "62445c4d922d127512867246",
$or: [
{
  filters: {
    "$elemMatch": {
      value: "626430bb19636d7db1804b78",
      name: "Valve Type "
    }
  }
},
{
  filters: {
    "$elemMatch": {
      value: "6264328519636d7db1804b86",
      name: "Material Type"
    }
  }
},
{
  filters: {
    "$elemMatch": {
      "value": "6264363319636d7db1804c11",
      "name": "Ball / Trim Material"
    }
  }
}
]
})

Playground 2

  • Related