I have a very nested set of mongoDB objects, and I want to count the number of subdocuments that match the given Change: condition (in each document) . For instance:
{"_id":{"chr":"20","pos":"14371","ref":"A","alt":"G"}, "studies":[ { "study_id":"Study1", "samples":[ { "sample_id":"NA00001", "formatdata":[ {"GT":"1|0","GQ":48,"DP":8,"HQ":[51,51]} ] }, { "sample_id":"NA00002", "formatdata":[ {"GT":"0|0","GQ":48,"DP":8,"HQ":[51,51]} ] } ] } ] } {"_id":{"chr":"20","pos":"14372","ref":"T","alt":"AA"}, "studies":[ { "study_id":"Study3", "samples":[ { "sample_id":"SAMPLE1", "formatdata":[ {"GT":"1|0","GQ":48,"DP":8,"HQ":[51,51]} ] }, { "sample_id":"SAMPLE2", "formatdata":[ {"GT":"1|0","GQ":48,"DP":8,"HQ":[51,51]} ] } ] } ] } {"_id":{"chr":"20","pos":"14373","ref":"C","alt":"A"}, "studies":[ { "study_id":"Study3", "samples":[ { "sample_id":"SAMPLE3", "formatdata":[ {"GT":"0|0","GQ":48,"DP":8,"HQ":[51,51]} ] }, { "sample_id":"SAMPLE7", "formatdata":[ {"GT":"0|0","GQ":48,"DP":8,"HQ":[51,51]} ] } ] } ] }
I want to know how many subdocuments GT contains: "1 | 0", which in this case will be 1 in the first document, and two in the second and 0 in the 3rd. I tried unwinding and aggregation functions, but I obviously am not doing anything right. When I try to count supporting documents by the βGTβ field, mongo complains:
db.collection.aggregate([{$group: {"$studies.samples.formatdata.GT":1,_id:0}}])
since my group names cannot contain ".", but if I leave them:
db.collection.aggregate([{$group: {"$GT":1,_id:0}}])
he complains because "$ GT cannot be the name of an operator"
Any ideas?