title: Count number of elements in an array
body: <p>I am creating an application, and it has a function which removes elements from an array inside a document in a MongoDB collection.</p>
  
  <p>I need a way to unset the array's field as soon as the array becomes empty right after a remove operation. I need to do this so that I can differentiate between a document that has a non-empty array, and a document that has an empty array.</p>
  
  <p>For example, right now my <code>section</code> collection looks like:</p>
  
  <pre><code>db.section.find({})
  
  { "_id" : ObjectId("57a0a38ad1c6ef24376477c5"), "sectionid" : "BTE4B", 
   "sectionname" : "BTech 4B", "year" : 4,
   "session" : 2016, "courseid" : "BTE-CS", "password" : "pm8xTE0-",
   "students" : 35, "addedon" : "2016-08-02 19:13:38", "teachers" : [ ] }
  
   { "_id" : ObjectId("57a0a96bd1c6ef24376477cd"), "sectionid" : "BTE4D",
   "sectionname" : "BTech 4D", "year" : 4, "session" : 2016, "courseid" :
   "BTE-CS", "password" : "sHhKr0Ov", "students" : 41, "addedon" :
   "2016-08-02 19:38:43", "teachers" : [ { "facultyid" : "CS-102",
   "subjectid" : "CS-ALGO" } ] }
  </code></pre>
  
  <p>I had applied a <em>$pull</em> operation using <em>mgo</em> on the <code>teachers</code> array of the first document, but I need a way to <code>$unset</code> it when it becomes empty.</p>
  
  <p>Currently, I have managed to create a workaround in Go, but I would like to get away without any workarounds.</p>
  
  <p>I hope I am not missing something very trivial here.</p>
  
  <p>Thanks!</p>