gpt4 book ai didi

PHP MongoDB 从结果中排除_id

转载 作者:行者123 更新时间:2023-12-02 04:54:37 25 4
gpt4 key购买 nike

我想使用 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
)

关于PHP MongoDB 从结果中排除_id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18221048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com