title: how to compare two fields in a document in pipeline aggregation (mongoDB)
body: <p>I have a document like below : </p>
  
  <pre><code>{
    "user_id": NumberLong(1),
    "updated_at": ISODate("2016-11-17T09:35:56.200Z"),
    "created_at": ISODate("2016-11-17T09:35:07.981Z"),
    "banners": {
      "normal_x970h90": "/images/banners/4/582d79cb3aef567d64621be9/photo-1440700265116-fe3f91810d72.jpg",
      "normal_x468h60": "/images/banners/4/582d79cb3aef567d64621be9/photo-1433354359170-23a4ae7338c6.jpg",
      "normal_x120h600": "/images/banners/4/582d79cb3aef567d64621be9/photo-1452570053594-1b985d6ea890.jpg"
    },
    "name": "jghjghjghj",
    "budget": "2000",
    "plan": null,
    "daily_budget": "232323",
    "daily_budget_auto": "",
    "href": "qls2.ir",
    "targets": {
      "cats": [
        "fun",
        "news"
      ],
      "region": "inIran",
      "iran_states": null,
      "os": "all",
      "gold_network": true,
      "dont_show_between_1_n_8": true
    },
    "payment": {
      "bank": "mellat",
      "tax": "add"
    },
    "click_cost": "102000",
    "status": null
  }
  </code></pre>
  
  <p>I want to check if <code>budget</code> is lower than <code>click_cost</code> while I'm checking some other parameters in my query :</p>
  
  <pre><code>db.bcamp.aggregate(
      [
          {
              $match:{
                  $and: [ 
                      {"targets.cats":{
                          "$in" : ["all"]
                          }
                      },
  
                      {"banners.normal_x970h90":{
                          "$exists":true
                          }
                      },
  
                      {"href": {
                          $nin: ["qls.ir"]
                          }
                      }
                  ]
              }
          }
      ]).pretty();
  </code></pre>
  
  <p>I have tried compare method like : </p>
  
  <pre><code>db.bcamp.aggregate(
      [
          {$project: {ab: {$cmp: ['$budget','$clickcost']}}},
          {$match: {ab:{$gt:1}}}
      ]).pretty();
  </code></pre>
  
  <p>But I was getting wrong result, it returns always 4 document which their budget may or may not be grater than click_cost, which it means it fetch wrong data.</p>
  
  <p>How can I add that comparison to my mongoDB pipeline? </p>