0

I am using jenssegers/laravel-mongodb

I was wondering if I can do this with mongodb aggregate. I have a dataset of enter image description here

And I am trying to achieve this kind of result or anything close that I can loop into enter image description here

What I have right now is this:

$collegeGender = FormData::raw( function ( $collection ) {
        return $collection->aggregate([
                ['$sortByCount' => [
                    '$mergeObjects' =>[
                        '$details.college',
                        '$details.gender'
                    ]
                ]]
            ]);
    });

But it is displaying an error:

$mergeObjects requires object inputs, but input "M" is of type string

Here is a sample of my collection data

{
   '_id': '5d5d77de54f57118ac003b46',
   'details': {
      'age' : 'M',
      'college' : 'College 1',
   }
}

I also don't know if I am using the aggregate function correctly :)

Kiel
  • 483
  • 3
  • 5
  • 18

1 Answers1

0

'age' and 'college' are not objects, they're just string fields, so you can't merge them using $mergeObjects instead you should use $concat

Mustafa Toker
  • 344
  • 3
  • 13