title: MongoDB: "$where is not allowed inside of a $match aggregation expression
body: <p>I got this error</p>
  
  <blockquote>
    <p>"$where is not allowed inside of a $match aggregation expression</p>
  </blockquote>
  
  <p>when I execute the following query.</p>
  
  <pre><code>var query = { $and : [
      {"order.orderstatus" : "F"},
      {"partsupp.supplier.nation.name" : { $regex : '^SAUDI ARABIA'} },
      {$where : "this.receiptdate &gt; this.commitdate"}
  ]};
  
  var multisupp = {
      $where : function() {
          return db.lineitems.findOne({ $and : [
              { "order.orderkey" : this.order.orderkey },
              { "partsupp.supplier.suppkey" : { "$ne" : this.partsupp.supplier.suppkey} }
          ]}) != null;
      }
  };
  
  var onlyfail = {
      $where : function() {
          return db.lineitems.findOne({ $and : [
              { "order.orderkey" : this.order.orderkey },
              { "partsupp.supplier.suppkey" : { "$ne" : this.partsupp.supplier.suppkey} },
              { $where : "this.receiptdate &gt; this.commitdate" }
              ]}) == null;
      }
  };
  
  res = db.lineitems.aggregate([
      { $match : query},
      { $match : multisupp},
      { $match : onlyfail},
      { $project : {
          _id : 0,
          s_name : "$partsupp.supplier.name" } },
      { $group : {
          _id : "$s_name",
          numwait : { $sum : 1} } },
      { $sort : { numwait : -1, _id : 1} }
  ]);
  </code></pre>
  
  <p>and the collection schema is this. (This collection has a huge schema but I guess that is not important for that problem)</p>
  
  <pre><code>{
      "linenumber": 6,
      "quantity": 17,
      "extendedprice": 21085.78,
      "discount": 0.08,
      "tax": 0.01,
      "returnflag": "R",
      "linestatus": "F",
      "shipdate": "Wed Oct 20 00:00:00 BST 1993",
      "commitdate": "Thu Dec 09 00:00:00 GMT 1993",
      "receiptdate": "Sun Nov 14 00:00:00 GMT 1993",
      "shipinstruct": "COLLECT COD",
      "shipmode": "REG AIR",
      "comment": "ns. carefully regular asym",
      "order": {
          "orderkey": 535110,
          "orderstatus": "F",
          "totalprice": 200883.33,
          "orderdate": "Fri Oct 01 00:00:00 BST 1993",
          "orderpriority": "1-URGENT",
          "clerk": "Clerk#000000110",
          "shippriority": 0,
          "comment": "furiously against the regular ideas; quickly regular dep",
          "order": {
              "custkey": 6464,
              "name": "Customer#000006464",
              "address": "eF9E6ScHCw9,z8nF0py9 ySlB0 iHTIEEZRWl6H",
              "phone": "11-870-572-9943",
              "acctbal": 5468.53,
              "mktsegment": "FURNITURE",
              "comment": "telets could are quickly regular packages. fluffily iro",
              "customer": {
                  "nationkey": 1,
                  "name": "ARGENTINA",
                  "comment": "al foxes promise slyly according to the regular accounts. bold requests alon",
                  "region": {
                      "regionkey": 1,
                      "name": "AMERICA",
                      "comment": "hs use ironic, even requests. s"
                  }
              }
          }
      },
  "partsupp": {
      "availqty": 9067,
      "supplycost": 441.56,
      "comment": ". bold instructions are carefully blithely final foxes. slyly unusual ideas wake slowly aroun",
      "part": {
          "partkey": 340,
          "name": "sienna purple lawn coral navy",
          "mfgr": "Manufacturer#2",
          "brand": "Brand#23",
          "type": "STANDARD BRUSHED TIN",
          "size": 3,
          "container": "JUMBO BOX",
          "retailprice": 1240.34,
          "comment": "l ideas wake. quic"
      },
      "supplier": {
          "suppkey": 91,
          "name": "Supplier#000000091",
          "address": "YV45D7TkfdQanOOZ7q9QxkyGUapU1oOWU6q3",
          "phone": "13-604-986-9056",
          "acctbal": 6255.87,
          "comment": "nstructions use carefully according to the special packages: quickly silent th",
          "nation": {
              "nationkey": 3,
              "name": "CANADA",
              "comment": "eas hang ironic, silent packages. slyly regular packages are furiously over the tithes. fluffily bold",
              "region": {
                  "regionkey": "1",
                  "name": "AMERICA",
                  "comment": "hs use ironic, even requests. s"
              }
          }
      }
  }
  </code></pre>
  
  <p>Can someone help me to solve that? Thank you.</p>