jeyzorus
jeyzorus

Reputation: 125

mongodb agregate and filter data

I try to filter some results data from mongodb with mongoose in javascript.

This is my json structure:

{
  "name": "john",
  "firstname": "doe",
  "yearold": 22,
  "recipes": [
    {
      "title": "cheesecake",
      "data": [
        {    
          "name": "egg",
          "label": "Eggs for",
          "value": 6,
          "unit": "piece"
        },
        {          
          "name": "oil",
          "label": "Specific oil",
          "unit": "oz",
          "value": 0.2
        },
        {          
          "name": "flour",
          "label": "Wholemel flour",
          "value": 450,
          "unit": "gr"
        }
      ]
    },
    {
      "title": "cake",
      "data": [
         {    
          "name": "egg",
          "label": "Eggs for",
          "value": 6,
          "unit": "piece"
        },
        {          
          "name": "flour",
          "label": "Wholemel flour",
          "value": 500,
          "unit": "gr"
        },        
      ]
    }
  ]
}

In some case i need to return json data with hiding some values. For example I have a list that specifies all the values ​​to hide

hidekeys=["egg"];

and i would like to get this:

{
      "name": "john",
      "firstname": "doe",
      "yearold": 22,
      "recipes": [
        {
          "title": "cheesecake",
          "data": [
            {    
              "name": "egg",
              "label": "Eggs for",
              "value": #######,
              "unit": "piece"
            },
            {          
              "name": "oil",
              "label": "Specific oil",
              "unit": "oz",
              "value": 0.2
            },
            {          
              "name": "flour",
              "label": "Wholemel flour",
              "value": 450,
              "unit": "gr"
            }
          ]
        },
        {
          "title": "cake",
          "data": [
             {    
              "name": "egg",
              "label": "Eggs for",
              "value": #######,
              "unit": "piece"
            },
            {          
              "name": "flour",
              "label": "Wholemel flour",
              "value": 500,
              "unit": "gr"
            },        
          ]
        }
      ]
    }

For each recipe i need to hide ingredient value if it is specified in hidekeys.

I tried something with $project and $cond but it doesnt works

Upvotes: 1

Views: 67

Answers (1)

Tom Slabbaert
Tom Slabbaert

Reputation: 22296

Here's a quick way of how to achieve this using $map

const hidekeys = ["egg"];
db.collection.aggregate([
  {
    $addFields: {
      recipes: {
        $map: {
          input: "$recipes",
          as: "recipe",
          in: {
            $mergeObjects: [
              "$$recipe",
              {
                data: {
                  $map: {
                    input: "$$recipe.data",
                    as: "datum",
                    in: {
                      "$mergeObjects": [
                        "$$datum",
                        {
                          $cond: [
                            {
                              "$setIsSubset": [
                                [
                                  "$$datum.name"
                                ],
                                hidekeys
                              ]
                            },
                            {
                              value: "#####"
                            },
                            {
                              value: "$$datum.value"
                            }
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
])

Mongo Playground

Upvotes: 1

Related Questions