Skip to content

Instantly share code, notes, and snippets.

@gatesvp
Created May 9, 2011 17:54
Show Gist options
  • Select an option

  • Save gatesvp/962975 to your computer and use it in GitHub Desktop.

Select an option

Save gatesvp/962975 to your computer and use it in GitHub Desktop.
PHP test sorting on sub-document
<?php
try{
$m = new Mongo('localhost');
$db = $m->abc;
$collection = $db->def;
$collection->drop();
// Insert test data
$collection->insert( array('A' => 1, 'B' => 4, 'C' => array("C1" => 'val', "C2" => 15) ) );
$collection->insert( array('A' => 2, 'B' => 5, 'C' => array("C1" => 'val', "C2" => 5) ) );
$collection->insert( array('A' => 3, 'B' => 6, 'C' => array("C1" => 'val', "C2" => 10) ) );
$cursor = $collection->find(array(), array('C.C2'))->sort(array('C' => -1))->limit(1);
foreach($cursor as $data){
print($data['C']['C2']);
}
}
catch(Exception $e){
print_r($e->getMessage());
print("\n");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment