swordfish
swordfish

Reputation: 4995

using php to fetch a record based on "_id" from mongo db

I am trying to fetch a record from mongo db using php. I am trying to use _id field in the mongo collection to access the record. But i am unable to fetch the corresponding record and i am only getting null. This is the code i am using.

$db =  $this->Generalmodel->connect();
$collection = $db->listing;
$cursor = $collection->findOne(array("_id"=>$id));
var_dump($cursor);

I also tried it with the following and is of no use too

$collection = $db->listing;
$cursor = $collection->find(array( '$and' => array( array('_id' => $id), array('blocked' => "0"))));
foreach ($cursor as $obj)
var_dump($obj)

Am i missing something here?

Upvotes: 1

Views: 1531

Answers (1)

spacenick
spacenick

Reputation: 1185

array('_id' => new MongoID($id))

Upvotes: 2

Related Questions