PHP MongoDB从结果中排除_id


PHP MongoDB Exclude _id from results

我想使用PHP从mongodb搜索结果中排除_id字段。如果我使用$param作为第二个搜索参数,它可以工作,但_id显然会被打印出来。这是我的代码:

$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 ) 

如果我使用以下代码_id也被打印出来,不知道我缺少了什么:

$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.

提前谢谢。

您没有正确发送投影(您试图返回的东西,find中的第二个参数)

所以不是这个

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

这是完全错误的,你必须键入

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

如果您已经在这里登录并正在使用更新的MongoDB库,您可能需要查看此处:MongoDB集合查找

你需要做这样的事情:

    $fields = array(
        'projection'=>array(
            '_id'=>0,
            'Derechos'=>1,
        )
    );