@@ -230,21 +230,27 @@ module.exports = {
230230 } ,
231231 } ,
232232 {
233- $unwind : {
234- path : '$roomDetails' ,
235- preserveNullAndEmptyArrays : true , // Preserve events without a corresponding room
233+ $project : {
234+ roomDetails : { $arrayElemAt : [ '$roomDetails' , 0 ] } ,
235+ } ,
236+ } ,
237+ {
238+ $group : {
239+ _id : '$roomDetails._id' ,
240+ name : { $first : '$roomDetails.name' } ,
241+ eventsCount : { $sum : 1 } ,
236242 } ,
237243 } ,
238244 {
239245 $group : {
240246 _id : null ,
241247 activeRooms : {
242248 $addToSet : {
243- _id : '$roomDetails. _id' ,
244- name : '$roomDetails. name' ,
249+ _id : '$_id' ,
250+ name : '$name' ,
245251 } ,
246252 } ,
247- eventsCount : { $sum : 1 } ,
253+ eventsCount : { $sum : '$eventsCount' } ,
248254 } ,
249255 } ,
250256 ] ,
@@ -276,21 +282,27 @@ module.exports = {
276282 } ,
277283 } ,
278284 {
279- $unwind : {
280- path : '$roomDetails' ,
281- preserveNullAndEmptyArrays : true , // Preserve messages without a corresponding room
285+ $project : {
286+ roomDetails : { $arrayElemAt : [ '$roomDetails' , 0 ] } ,
287+ } ,
288+ } ,
289+ {
290+ $group : {
291+ _id : '$roomDetails._id' ,
292+ name : { $first : '$roomDetails.name' } ,
293+ messagesCount : { $sum : 1 } ,
282294 } ,
283295 } ,
284296 {
285297 $group : {
286298 _id : null ,
287299 activeRooms : {
288300 $addToSet : {
289- _id : '$roomDetails. _id' ,
290- name : '$roomDetails. name' ,
301+ _id : '$_id' ,
302+ name : '$name' ,
291303 } ,
292304 } ,
293- messagesCount : { $sum : 1 } ,
305+ messagesCount : { $sum : '$messagesCount' } ,
294306 } ,
295307 } ,
296308 ] ,
0 commit comments