title: Mongodb Aggregate query to groovy script
body: <p>I am new to groovy scripting. I want to convert mongodb aggregate query to groovy script to query mongodb. Below is the mongodb query i want to change to groovy script:</p>
  
  <pre><code>db.VERISK_METADATA_COLLECTION.aggregate([
  { "$match":{contentType:"FRM"}},
  { "$project":{ "appCount": {$size:"$applicability"}}},
  { "$group": { "_id":null, Total: {$sum:"$appCount" } }},
  { "$project": {Total:1,"_id":0}}
  ])
  </code></pre>
  
  <p>Below is one sample record I am writing this query for:</p>
  
  <pre><code>{
      "_id" : "04c64247-7030-4b0e-a2f9-da77d27b8667",
      "_class" : "com.mongodb.BasicDBObject",
      "documentType" : "doc",
      "stateType" : "AZ",
      "availabilityDate" : "",
      "multiState" : "false",
      "language" : "E",
      "title" : "ARIZONA LIMITED EXCLUSION OF ACTS OF TERRORISM (OTHER THAN CERTIFIED ACTS OF TERRORISM); CAP ON LOSSES FROM CERTIFIED ACTS OF TERRORISM; COVERAGE FOR CERTAIN FIRE LOSSES",
      "displayFormNumber" : "FP 10 53 09 05",
      "mandatory" : "N",
      "client_id" : "VERISK_001",
      "ignored_record_type" : "new_publish_insert",
      "formLobType" : "FR",
      "formNumber" : "FP10530905",
      "job_queue_id" : "f8ac839f-24ee-4063-bc3c-7ed00eb91f11",
      "contentType" : "FRM",
      "directoryName" : "FRFORMS",
      "formsType" : "E",
      "objectTypeCode" : "13",
      "documentName" : "FP10539O",
      "doc_id" : [ 
          "f3f528e2-32ca-49f8-b287-b436befae779"
      ],
      "update_date" : "2017-12-27 21:05:21.189 EST",
      "earliestEffectiveDate_dt" : "2005-09-01T00:00:00Z",
      "formStatus" : "H",
      "applicability" : [ 
          {
              "filingId_dbValue_str" : "CL-2005-OFOTR",
              "jurisdiction" : "AZ",
              "withDrawnDate" : "2008-10-01T00:00:00Z",
              "filingId" : "CL-2005-OFOTR",
              "derivedFrom" : "",
              "id" : "0ef65042-6b35-460e-b12a-46766c64ff27",
              "circularNumber" : "LI-FR-2005-121",
              "lob" : "FR",
              "effectiveDate" : "2005-09-01T00:00:00Z",
              "circularDate" : "2005-07-15T00:00:00Z"
          }
      ],
      "created_date" : "2017-12-27 21:05:21.106 EST",
      "uri" : "VERISK_001/FRM/FP10530905"
  }
  </code></pre>