0

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?

3
  • what is MongoID() function?is that can reverse GameID (from 50fde048f1568a204c0002a1 to 3.1677054844223E+18) ? Commented Jan 22, 2013 at 1:42
  • Hmm your PHP driver does not seen to recognise objectIds at all for some unknown reason, what does the document look in your database? Can you paste a console output? Your _id may have changed type, if so then you cannot query by objectid Commented Jan 22, 2013 at 8:11
  • @Sammaye The console output is this: /* 0 / { "_id" : NumberLong("3292523252251813670"), "league" : "ncaab", "gameTime" : "2013-01-07T18:30:00", "homeTeam" : "Cincinnati", "homeRank" : 14, "awayTeam" : "Notre Dame", "awayRank" : 21 } / 1 */ { "_id" : NumberLong("6877183749884304734"), "league" : "ncaab", "gameTime" : "2013-01-07T19:00:00", "homeTeam" : "Penn St.", "awayTeam" : "Indiana", "awayRank" : 5 } Commented Jan 22, 2013 at 20:52

1 Answer 1

1

Yea some one has been fiddling with your ObjectIds and they are no longer ObjectIds.

You need to be using ObjectIds to query by them hence you cannot query by an ObjectId (MongoId in PHP).

You should be able to query without wrapping the figure in a MongoId though since, if you have native_long set 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 convert NumberLongs to and fro for you.

So literally just query by the $result['_id'] without putting it into a MongoId.

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 (MongoId in PHP) since this will be a lot more efficient and easier than the ID you are currently using.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I had to move to a linux 64-bit php env to get this working properly. Also suggested the monogo team use objectID's.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.