Database
[{"logs":{"events":{"type":"call_action","attributes":[{"key":"module","value":"module1"},{"key":"data","value":"data_value_module_1"},{"key":"module","value":"module2"},{"key":"data","value":"data_value_module_2"}]}},}]
Query
db.collection.aggregate([{$set:{"logs.events.attributes":{$reduce:{input:"$logs.events.attributes",initialValue:[],in:{$concatArrays:["$$value",[{k:"$$this.key",v:"$$this.value",group:{$floor:[{$divide:[{$size:"$$value"},2]}]}}]]}}}}},{$unwind:"$logs.events.attributes"},{$group:{_id:"$logs.events.attributes.group",data:{$push:{k:"$logs.events.attributes.k",v:"$logs.events.attributes.v"}},type:{$first:"$logs.events.type"}}},{$project:{_id:0,"logs.events":{attributes:{$arrayToObject:"$data"},type:"$type"}}}])