Return an entire document in yii2 mongo

Viewed 28

I have a nested document saved up in MongoDB and I wanted to fetch it all since I know the ID

I can select a few fields like this

public function actionFetchId()
{
    $data = json_decode(file_get_contents("php://input"), TRUE);

    $recordid = (string)$data['dh'];
  
    $collection = Yii::$app->mongodb->getCollection('properties');
   
   //Create Properties List Object
   
   $basic_info_query = new Query;
    
    $basic_info_query->select(['finalize'])     
        ->from('properties')
        ->where(['_id' => $recordid]);
    $rows = $basic_info_query->all();
    
    return json_encode($recordid);
}

and I can assemble my fields I want to fetch here

$basic_info_query->select(['finalize','basic_info'])

However, I would like to fetch all fields in the document. How can I fetch all document fields like in SQL select * from properties in Yii2 Mongo?

1 Answers

I was able to fetch the entire document like this

public function actionFetchId()
{
    $data = json_decode(file_get_contents("php://input"), TRUE);
    
    $recordid = (string)$data['dh'];
      
    $collection = Yii::$app->mongodb->getCollection('properties');
       
    //Create Properties List Object
       
    $fetch_all = new Query;
        
    $fetch_all->from('properties')
        ->where(['_id' => $recordid]);
    $rows = $fetch_all->all();
        
    return json_encode($rows);
}
Related