0

I'm using laravel-mongoDB how can I search in attraction and blog titles and only retrieve the object, not whole record. I've tried

[
'$match' => [
             "attraction.title" => 
              ['$regex' => '.*' . $request- >search . '.*']
     ],
],

result was whole record. for example if search is first, output should be :

[{title: "first attraction", complete: false},{title: "first blog", complete: false}]

and this is records

[attraction: [{title: "first attraction", complete: false}, {title:"second attraction", 
complete: true}]
blog: [{title: "first blog", complete: false}, {title: "second blog", complete: false}]
title: "test"
type: "internal"]
Reza Sam
  • 1,264
  • 2
  • 14
  • 29

1 Answers1

0

I didn't get the result with only just query but hope it helps

$data = Model::raw(function ($collection) use ($request) {
        return $collection->aggregate([
                ['$facet' =>
                    [
                        'attr' => [
                            [
                                '$match' => [
                                    "attraction.title" => ['$regex' => '.*' . $request->search . '.*'],
                                ],
                            ],
                            [
                                '$unwind' => '$attraction'
                            ],
                            [
                                '$project' => [
                                    'title' => '$attraction.title',
                                ]
                            ],
                            [
                                '$match' => [
                                    "title" => ['$regex' => '.*' . $request->search . '.*'],
                                ],
                            ]
                        ],
                        'blog' => [
                            [
                                '$match' => [
                                    "blog.title" => ['$regex' => '.*' . $request->search . '.*'],
                                ],
                            ],
                            [
                                '$unwind' => '$blog'
                            ],
                            [
                                '$project' => [
                                    'title' => '$blog.title',
                                ]
                            ],
                            [
                                '$match' => [
                                    "title" => ['$regex' => '.*' . $request->search . '.*'],
                                ],
                            ]
                        ]
                    ]
                ],


            ]

        );
    });

$data will be like:

  {
     attr:[
           {title:...},
           {title:...}
           ],
      blog:[
           {title:...},
           {title:...}
           ]
  }

and use iterator_to_array and foreach to get final result

Reza Sam
  • 1,264
  • 2
  • 14
  • 29