Nida Amin
Nida Amin

Reputation: 755

mongo db dynamic query is formed in wrong way in php

I am using below php code for creating dynamic queries in mongo db like

        ..........
        if ($this->programId != "") {
          $query['classId'] = new MongoDB\BSON\ObjectID($this->programId);
        }
          
        ......
       
        $transportArrayCondition1 = ['transport_details' =>  ['$size' => 0]];
        $transportArrayCondition2 =   ['transport_details' =>  ['$elemMatch' =>  [
           '$or' => [
                    ['status' => ['$eq' => 'Requested']],
                    ['status' => ['$eq' => 'Active']],
            ],
            "approval_status" => "approved",
            "allotable" => "yes",
        ]]];

        $conditionToApply = '$or';

        if($this->routeId != "")
        {
          $transportArrayCondition1 = ['transport_details' => ['$gt' => ['$size' => 0]]];
          $transportArrayCondition2 =  ['transport_details' =>  ['$elemMatch' =>  [
            '$or' => [
              ['status' => ['$eq' => 'Requested']],
              ['status' => ['$eq' => 'Active']],
            ],
            "approval_status" => "approved",
            "allotable" => "yes",
            "route_id" =>  new MongoDB\BSON\ObjectID($this->routeId)
          ]]];

           $conditionToApply = '$and';
        }

        if ($this->stopId != "") 
        {
          $transportArrayCondition1 = ['transport_details' => ['$gt' => ['$size' => 0]]];
          $transportArrayCondition2 =  ['transport_details' =>  ['$elemMatch' =>  [
            '$or' => [
              ['status' => ['$eq' => 'Requested']],
              ['status' => ['$eq' => 'Active']],
            ],
            "approval_status" => "approved",
            "allotable" => "yes",
            "route_id" =>  new MongoDB\BSON\ObjectID($this->routeId),
            "stop_id" => $this->stopId
          ]]];

          $conditionToApply = '$and';
        }

      $query[$conditionToApply] = [$transportArrayCondition1, $transportArrayCondition2];

      if ($this->name != "") {

            $nameArray = ['$or' => [
              ['fullName' => new MongoDB\BSON\Regex('^' . $this->name, 'i')],
              ['firstLastName' => new MongoDB\BSON\Regex('^' . $this->name, 'i')],
              ['registration_temp_perm_no' => new MongoDB\BSON\Regex('^' . $this->name, 'i')],
            ]];

             array_push($query,$nameArray);
      } 

Actually, I have put conditions in $query variables for MongoDB commands. I am failing to create condition in if '$this->name != "" ' part of the query. I am getting $query formed like

          {
"0": {
    "$or": [
        {
            "fullName": {
                "$regex": "^Vika",
                "$options": "i"
            }
        },
        {
            "firstLastName": {
                "$regex": "^Vika",
                "$options": "i"
            }
        },
        {
            "registration_temp_perm_no": {
                "$regex": "^Vika",
                "$options": "i"
            }
        }
    ]
},
"schoolId": {
    "$oid": "5f7aba204c610000670026d2"
},
"status": "Active",
"activeAcademicyearId": {
    "$oid": "5f7abaa54c61000067002738"
},
"$or": [
    {
        "transport_details": {
            "$size": 0
        }
    },
    {
        "transport_details": {
            "$elemMatch": {
                "$or": [
                    {
                        "status": {
                            "$eq": "Requested"
                        }
                    },
                    {
                        "status": {
                            "$eq": "Active"
                        }
                    }
                ],
                "approval_status": "approved",
                "allotable": "yes"
            }
          }
          }
         ]
        }
        

What I actually want is

{
"$or": [
    {
        "fullName": {
            "$regex": "^Vika",
            "$options": "i"
        }
    },
    {
        "firstLastName": {
            "$regex": "^Vika",
            "$options": "i"
        }
    },
    {
        "registration_temp_perm_no": {
            "$regex": "^Vika",
            "$options": "i"
        }
    }
]
   "schoolId": {
    "$oid": "5f7aba204c610000670026d2"
  },
   "status": "Active",
   "activeAcademicyearId": {
    ............
      

I want array_push should add condition in $query like "$or": [...]. It should not add condition like "0": {"$or": [... ]}, I have tried a lot of things since morning but could not find any solution. Kindly help !!!

Upvotes: 0

Views: 56

Answers (1)

Chandan
Chandan

Reputation: 11807

Since $query is associated array like structure and it already contains $or key in which you want to add the element you may need to update it as below.

Change $nameArray as below

$nameArray = [
  ['fullName' => new MongoDB\BSON\Regex('^' . $this->name, 'i')],
  ['firstLastName' => new MongoDB\BSON\Regex('^' . $this->name, 'i')],
  ['registration_temp_perm_no' => new MongoDB\BSON\Regex('^' . $this->name, 'i')],
];

array_push add elements add the end of the array

If you want to merge with existing $or values then we can use array_merge to merge both as array and then reassign it to existing $or key in $query

$query['$or'] = array_merge($nameArray, $query['$or']);

Or

$query['$or'] = $nameArray + $query['$or'];

If you want to replace then you can just reassign the changed $nameArray variable to $query['$or']

$query['$or'] = $nameArray;

Upvotes: 1

Related Questions