Mongodb count() of internal array -
i have following mongodb collection db.students:
/* 0 */ { "id" : "0000", "name" : "john" "subjects" : [ { "professor" : "smith", "day" : "monday" }, { "professor" : "smith", "day" : "tuesday" } ] } /* 1 */ { "id" : "0001", "name" : "mike" "subjects" : [ { "professor" : "smith", "day" : "monday" } ] }
i want find number of subjects given student. have query:
db.students.find({'id':'0000'})
that return student document. how find count 'subjects'? doable in simple query?
if query return 1 element :
db.students.find({'id':'0000'})[0].subjects.length;
for multiple elements in cursor :
db.students.find({'id':'0000'}).foreach(function(doc) { print(doc.subjects.length); })
do not forget check existence of subjects either in query or before check .length
Comments
Post a Comment