MongoDB nested object aggregation counting

后端 未结 1 2079
情歌与酒
情歌与酒 2021-02-06 07:38

I have a highly nested mongoDB set of objects and I want to count the number of subdocuments that match a given condition Edit: (in each document). For example

相关标签:
1条回答
  • 2021-02-06 07:59

    You need to process $unwind when working with arrays, and you need to do this three times:

     db.collection.aggregate([
    
         // Un-wind the array's to access filtering 
         { "$unwind": "$studies" },
         { "$unwind": "$studies.samples" },
         { "$unwind": "$studies.samples.formdata" },
    
         // Group results to obtain the matched count per key
         { "$group": {
             "_id": "$studies.samples.formdata.GT",
             "count": { "$sum": 1 }
         }}
     ])
    

    Ideally you want to filter your input. Possibly do this with a $match both before and after $unwind is processed and using a $regex to match documents where the data at point begins with a "1".

     db.collection.aggregate([
    
         // Match first to exclude documents where this is not present in any array member
         { "$match": { "studies.samples.formdata.GT": /^1/ } },
    
         // Un-wind the array's to access filtering 
         { "$unwind": "$studies" },
         { "$unwind": "$studies.samples" },
         { "$unwind": "$studies.samples.formdata" },
    
         // Match to filter
         { "$match": { "studies.samples.formdata.GT": /^1/ } },
    
         // Group results to obtain the matched count per key
         { "$group": {
             "_id": {
                  "_id": "$_id",
                  "key": "$studies.samples.formdata.GT"
             },
             "count": { "$sum": 1 }
         }}
     ])
    

    Note that in all cases the "dollar $" prefixed entries are the "variables" referring to properties of the document. These are "values" to use an input on the right side. The left side "keys" must be specified as a plain string key. No variable can be used to name a key.

    0 讨论(0)
提交回复
热议问题