2021-03-01

MongoDB - Distribution of number of documents per field count

db.users.aggregate([

    {$project:{

        "field_count": {$size: {$objectToArray:"$$ROOT"}}

    }},

    {$group:{

        "_id": {field_count: "$field_count"},

        "document_count": {$sum: NumberInt(1)},

    }},

    {$project:{

        "field_count": "$_id.field_count"

        , "document_count": 1

        , "_id": 0

    }},

    {$sort: {

        "field_count": -1

    }}

]);


PS. In other words, how many documents exist for each field count (e.g. "78 documents have 5 fields"). 

No comments:

Post a Comment