Saikumar A
Saikumar A

Reputation: 223

how to exclude mongodb _id field in php result

I'm trying to exclude the _id field from PHP-MongoDB result.below is my PHP script.What I can add to the script to exclude _id from the output.

<?php
try
{
    $connection = new Mongo('mongodb://user:[email protected]:xxxxx/stats');
    $database   = $connection->selectDB('stats');
    $collection = $database->selectCollection('stat');
}
catch(MongoConnectionException $e)
{
    die("Failed to connect to database ".$e->getMessage());
}

$cursor = $collection->find();

echo json_encode(iterator_to_array($cursor));



?>

Upvotes: 0

Views: 526

Answers (1)

Mr Davros
Mr Davros

Reputation: 98

Add:-

$cursor->fields(array('_id'=>false));

After the find...

Upvotes: 1

Related Questions