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

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -