title: How to use $subtract with array of document in mongoDB
body: <p>We have two collections in which have <strong>users</strong> and <strong>organizations</strong>.</p>
  
  <pre><code>db.users.aggregate([
     {$match: {"organization.metaInfo":{"$ne": "disabled"}}},
     {"$unwind":"$organization"},
     {"$group":{"_id":"$organization.organizationId", "count":{"$sum":1}}},
     {$lookup: {from: "organisations",
                localField: "_id",
               foreignField: "_id", as: "orgDta"}},
     {"$project":{"_id":1, "count":1, 
                "orgDta.organizationLicence.userCount":1     
     }}
  
  ])
  </code></pre>
  
  <p>When this query is performed return a result like which is good to me.</p>
  
  <pre><code>{
    "_id" : "768d3090-d4f5-11e7-a503-9b68b90cdb4e",
    "count" : 5.0,
    "orgDta" : [{ 
             "organizationLicence" : {
                "userCount" : 50
            }
      }]
  },
  {
    "_id" : "d9933740-c29c-11e7-9481-b52c5f3e2e70",
    "count" : 1.0,
    "orgDta" : [{
            "organizationLicence" : {
              "userCount" : 1
          }
      }] 
   },
   {
     "_id" : "5386ebc0-c29b-11e7-9481-b52c5f3e2e70",
      "count" : 1.0,
      "orgDta" : [{
             "organizationLicence" : {
               "userCount" : 1
           }
      }]
   }
  </code></pre>
  
  <p><strong>Now, I want to perform a subtract operation in between count and userCount.But I don't know that how to use here.</strong></p>
  
  <p>I was trying together with <strong>$project</strong></p>
  
  <blockquote>
    <p>{"$project":{"_id":1, "count":1, "orgDta.dObjects":1, "orgDta.organizationLicence.userCount":1, 
     "remainingUser": { $subtract: [ "$orgDta.organizationLicence.userCount", "$count"]}</p>
  </blockquote>
  
  <p>But Mongo returns error</p>
  
  <blockquote>
    <p>{
        "message" : "cant $subtract adouble from a array",
        "stack" : "MongoError: cant $subtract adouble from a array" }</p>
  </blockquote>