Posted under » MongoDB on 29 August 2024
To count embedded objects use the len method. After you get the len then you can iterate.
myquery = { "cid" : 2118 } for x in mthlog.find(myquery): howmany = len(x["band-06"]) print(howmany) for i in range(howmany): pprint.pprint(x["band-06"][i]['band'])
len will only work if the list do exist. Otherwise there will be a 'key' error. To be safe, it is best that you make sure, there is something to len with by
myquery = { "cid" : 2118, "band-06" : {"$ne" : None} } for x in mthlog.find(myquery): howmany = len(x["band-06"]) print(howmany)
Another issue is that "band-06" must be consistent. You cannot have list and then integer.
You can't len an integer.