sms sms - 6 days ago 5
PHP Question

PHP MongoDB Exclude _id from results

I want to exclude _id field from mongodb search results using PHP.
If I use $param as second search parameter it works but _id obviously gets printed.
Here is my code:

$type['Tipo de objeto']=$_POST[type];
$param = $_POST[field];
$conn = new Mongo();
$db = $conn->selectDB('prov');
$results = $db->$table->find($type, $param);

print_r($param) // Array ( [0] => Título [1] => Descripción [2] => Actos [3] => Formato original [4] => Título [5] => Descripción )


If i use the following code _id gets printed too, don't know what I`m missing:

$results = $db->$table->find($type, array("_id : 0" , "Derechos : 1", "Actos : 0")); //Fields marked with 1 gets printed, fields marked with 0 doesn't, except for _id.


Thanks in advance.

Answer

You are not sending correctly your projection (the thing you are trying to return, second parameter in find)

So instead of this

array(
  "_id : 0" , 
  "Derechos : 1",
  "Actos : 0"
)

which is totally wrong, you have to type

array(
  "_id" =>  0, 
  "Derechos" => 1
)