I’m using a windows xampp server running PHP 5.4 and the PHP Mongo driver from here.
I am querying the data using:
$results = $collection->find( array('league'=>'nba') );
foreach ($results as $user) {
var_dump($user);
echo "</br>";
}
Result:
array(5) { ["_id"]=> float(3.1677054844223E+18) ["league"]=> string(3) "nba" ["homeTeam"]=> string(10) "Washington" ...
I am converting the ID to a string:
$cursorID = new MongoID($result['_id']);
$gameLink = "<a href='/home/game/".$cursorID."'>".$cursorID."</a>";
which converts the ID from a float to this value: 50fde048f1568a204c0002a1
and trying to query for game details:
$gameID = new MongoID("50fde048f1568a204c0002a1"); //default
$con = new Mongo("mongodb://mongo.example.net"); // Connect to Mongo Server
$db = $con->selectDB('mydb');
$games = $db->games->find( array('_id'=>$gameID) );
But this does not return any results.
Thoughts on what could be going wrong?
Yea some one has been fiddling with your
ObjectIds and they are no longerObjectIds.You need to be using
ObjectIds to query by them hence you cannot query by anObjectId(MongoIdin PHP).You should be able to query without wrapping the figure in a
MongoIdthough since, if you havenative_longset in your php.ini: http://php.net/manual/en/mongo.configuration.php#ini.mongo.native-long (which it is by default in the newer drivers I bellieve), the driver should automatically convertNumberLongsto and fro for you.So literally just query by the
$result['_id']without putting it into aMongoId.A word of caution though, using the _id you are not really that good and I would recommend you re-populate all your records again using
ObjectId(MongoIdin PHP) since this will be a lot more efficient and easier than the ID you are currently using.