If condition is never satisfied MongoDB

  Kiến thức lập trình

I have written a simple query in MongoDB but if condition is never satisfied and always “then” is executed. “Else” condition never works. Here is my code :

[
  {
    $lookup: {
      from: "Author",
      localField: "auth_id",
      foreignField: "auth_id",
      as: "author_info",
    },
  },
  {
    $project: {
      _id: 1,
      name: 1,
      "author_info.name": 1,
      "author_info.lastname": 1,
      price: {
        $cond: {
          if: {
            $gt: ["price", 35],
          },
          then: {
            $subtract: ["$price", 10],
          },
          else: {
            $add: ["$price", 5],
          },
        },
      },
    },
  },
  {
    $unwind: {
      path: "$author_info",
      preserveNullAndEmptyArrays: false
    }
  }
]

For example, regardless that price is greater than 35, code always substracts 10 and never adds 5. Can you please point out my error ? Any help would be much appreciated.

LEAVE A COMMENT