I get data from mongodb in php and display fields in table. Can I get fields in php from mongodb in the order I set, for example, when the order of fields I send in fields doesn’t effect othe order of fields I display them:
$cursor = static::getMongoCollection(false)->find($query)->fields($fields);
For example it displays in the same order now matter if I use:
$fields = array("field1" => 1, "field2" => 1, "field3" => 1)
or
$fields = array("field3" => 1, "field2" => 1, "field1" => 1)
I need in my application to display fields in different order. Is there a way to make it?
The PHP driver just returns the fields from MongoDB in whatever order MongoDB sends them. So, regardless of how you select the fields you wish to return, the data sent back won’t change (unless you alter the fields list of course). Why don’t you just order the results yourself after they are returned?