bsonSize in aggregation

Hi,

I saw that mongo 4.4 added that ability to get bsonSize inside aggregation but I’m working with mongo 4.2
I wanted to know if there is any way to get the size of a field (or the full document) with aggregation.

This is the query I want to execute

db.temp_coll.aggregate([
{
    "$match": {}
},
{
    "$addFields": { "size": {$bsonSize: "$$ROOT"}}
},
{
    "$group": {
        "_id": {
            "groupField": "$groupField",
        },
        "count": {"$sum": 1},
        "totalSizeBytes": {"$sum": "$size"},
        "maxId": {"$max": "$_id"},
        "minId": {"$min": "$_id"},
    }
}
])

I know there is Object.bsonsize but not sure how can I access (if I can) to a value of a field in the current document (when I do it before the grouping.

Unfortunately you cannot use Aggregation to accomplish this in MongoDB 4.2 and earlier.

You can however use a Map-Reduce command to perform this operation as follows:

// Setup
db.foo.drop();
db.foo.insert({ _id: 1, groupField: "a", data: "abcdef" })
db.foo.insert({ _id: 2, groupField: "b", data: "abcdefghijklmnop" })
db.foo.insert({ _id: 3, groupField: "a", data: "abcdefghijklmnopqrstuvwxyz" })
db.foo.insert({ _id: 4, groupField: "b", data: "abc" })
db.foo.insert({ _id: 5, groupField: "a", data: "abcdefghij" })
// Map-Reduce
db.runCommand({
    mapReduce: "foo",
    map: function(){ 
        emit(this.groupField, this) 
    },
    reduce: function(key, values) { 
        var ret = { count: 1, totalSizeBytes: Object.bsonsize(values[0]), maxId: values[0]._id, minId: values[0]._id };
        var max = ret.totalSizeBytes;
        var min = ret.totalSizeBytes;
        for (var i = 1; i < values.length; i++) {
            var doc = values[i];
            var size = Object.bsonsize(doc);
            ret.totalSizeBytes += size;
            ret.count++;
            if (size > max) { max = size; ret.maxId = doc._id }
            if (size < min) { min = size; ret.minId = doc._id }
        }
        return ret;
    },
    out: { inline: 1 } 
})
// Output
{
	"results" : [
		{
			"_id" : "a",
			"value" : {
				"count" : 3,
				"totalSizeBytes" : 183,
				"maxId" : 3,
				"minId" : 1
			}
		},
		{
			"_id" : "b",
			"value" : {
				"count" : 2,
				"totalSizeBytes" : 113,
				"maxId" : 2,
				"minId" : 4
			}
		}
	],
	"timeMillis" : 23,
	"counts" : {
		"input" : 5,
		"emit" : 5,
		"reduce" : 2,
		"output" : 2
	},
	"ok" : 1
}