Im using MongoDB with laravel and trying to get data using $lookup, but its not working. I have 2 collection, one is states
with '_id','state_id','state_name'
and another collection is category
with '_id','name','description','state_id'
. So with lookup im trying to get state_name from states Collection, Below is my query.
$query=[ ['$project'=> [ "_id"=>1, "name" => 1, "description"=>1, "state_id"=>1, "data"=>1, "state_name"=>1, // trying to project this using lookup ]], ['$lookup' => [ 'from' => 'category', 'localField' => 'state_id', 'foreignField' => 'state_id', 'as' => 'states_docs', ]], [ '$unwind'=> '$data' ], [ '$group'=>[ '_id'=>'$_id', 'name'=> [ '$first'=> '$name' ], 'description'=>['$first'=>'$description'], 'state_id'=>['$first'=>'$state_id'], 'data'=>['$first'=>'$data'], 'state_name'=>['$first'=>'$state_name'], ] ], ]; //query to database $result = $collection->aggregate($query); foreach($result as $row) { $data[]=[ '_id'=>$row->_id, 'name' => $row->name, 'description'=>$row->description, 'state_id'=>$row->state_id, 'state_name' => $row->state_name,//trying to get state_name using lookup ]; }
Please help me to get state_name in this data array.
https://stackoverflow.com/questions/65785347/lookup-is-not-working-in-laravel-using-mongodb January 19, 2021 at 12:08PM
没有评论:
发表评论