'how can I give order wise Projection in mongodb query
I am new to monngodb, my query is below
$mng = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$filter = [ "archival"=>0];
$options = [ 'projection' => [ 'created_date'=> 1, 'firstname' => 1, 'phone_number' => 1, 'skill_set' => 1, 'additional_skill_set' => 1, 'total_experience' => 1, 'current_annual_ctc' => 1, 'expected_annual_ctc' => 1, 'notice_period' => 1, 'employement_type' => 1,'location' => 1, 'user' => 1, 'lead_source' => 1, 'current_employer' => 1, 'gender' => 1],'sort' => ['created_date' => -1]]; //,"limit" => 10
$qry = new MongoDB\Driver\Query($filter,$options);
$collection = $mng->executeQuery("testdb.testcollection", $qry);
$exD1_User = $collection->toArray();
$D1_UserArray = json_decode(json_encode($exD1_User), true);
$data = array();
when I run this I am getting different order like shown below
created_date
firstname
phone_number
user
skill_set
additional_skill_set
total_experience
current_annual_ctc
current_employer
expected_annual_ctc
notice_period
location
employement_type
gender
lead_source
what ever I have given is different. Could you please help on it how can I set order wise.
In mysql we can give fields name like shown below.
select col1,col2,col5 from test
the result will come in order like col1,col2,col5
chosen fields,like this how can I do in Mongodb .
Thanks
sandeep
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|