I am creating an application, and it has a function which removes elements from an array inside a document in a MongoDB collection.
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.
For example, right now my section
collection looks like:
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" } ] }
I had applied a $pull operation using mgo on the teachers
array of the first document, but I need a way to $unset
it when it becomes empty.
Currently, I have managed to create a workaround in Go, but I would like to get away without any workarounds.
I hope I am not missing something very trivial here.
Thanks!