colinmollenhour / mongodb-php-odm

A simple but powerful set of wrappers for using MongoDb in PHP (also a Kohana 3 module)
BSD 3-Clause "New" or "Revised" License
208 stars 50 forks source link

having problem searching through id #4

Closed idenoq closed 14 years ago

idenoq commented 14 years ago

I have been trying to implement query like this: db.posts.find({"_id" : ObjectId("4cb21a15a2749efc83ff831e")}); and I haven't had success. I have tried:

  1. $loaded_posts = $posts->find('{"_id" : ObjectId("4cb21a15a2749efc83ff831e")}');
  2. $loaded_posts = $posts->find('{"_id" : "4cb21a15a2749efc83ff831e"}'); First one creates exception collection.php "Unable to parse query from JSON string" Second one makes Apache eat all of my memory so there might be an endless loop somewhere.

When I create query in database - it works. When I try it in model based on yours it doesn't work. This happens only with native _id and not with other elements of document when I use "find" or "findone"...

colinmollenhour commented 14 years ago

I'll have to look into the second case. The first will not be supported since parsing the ObjectId out of the json just isn't worth it.. The equivalent to ObjectId in PHP is MongoId. See http://www.php.net/manual/en/class.mongoid.php

This will work: $loaded_posts = $posts->find(array('_id' => new MongoId("4cb21a15a2749efc83ff831e")));

idenoq commented 14 years ago

Thank you very much. I have found the same thing by my self 15min ago :) I have been using your module with ko3 for 3 days and testing performance compared to mysql, I am seriously thinking about using it for production. To all of you wo read this please look into http://www.php.net/manual/en/mongo.types.php. Thanks one more time.