Search code examples
node.jsmongodbaggregate

Unrecognized expression Error in Aggregation Projection Query


Invalid $project :: caused by :: Unrecognized expression '$$varientPricePoint.ppId'

{
  "pricepoint": {
    "$filter": {
      "input": "$OriginalPricePoints",
      "as": "varientPricePoint",
      "cond": {
        "$$varientPricePoint.ppId": {
          "$in": ["PP100"]
        }
      }
    }
  },
  "_id": 0
}

Solution

  • You can't use the variable as the root object key, instead you need wrap with $in operator as below:

    {
      "pricepoint": {
        "$filter": {
          "input": "$OriginalPricePoints",
          "as": "varientPricePoint",
          "cond": {
            $in: [
              "$$varientPricePoint.ppId",
              ["PP100"]
            ]
          }
        }
      }
    }